读取目标的lastModifiedTime和lastAccessTime属性,并将它们设置为链接。
import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.LinkOption; /** from 时 代 J a v a 公 众 号 - nowjava.com**/ import java.nio.file.Path; import java.nio.file.attribute.FileTime; public class Main { public static void main(String[] args) { Path link = FileSystems.getDefault().getPath("test"); Path target = FileSystems.getDefault().getPath("C:/folder1/photos", "test.jpg");/* 来自 N o w J a v a . c o m - 时 代 Java*/ try { Files.createSymbolicLink(link, target); FileTime lm = (FileTime) Files.getAttribute(target, "basic:lastModifiedTime", LinkOption.NOFOLLOW_LINKS); FileTime la = (FileTime) Files.getAttribute(target, "basic:lastAccessTime", LinkOption.NOFOLLOW_LINKS); Files.setAttribute(link, "basic:lastModifiedTime", lm, LinkOption.NOFOLLOW_LINKS); Files.setAttribute(link, "basic:lastAccessTime", la, LinkOption.NOFOLLOW_LINKS); } catch (IOException | UnsupportedOperationException | SecurityException e) { if (e instanceof SecurityException) { System.err.println("Permision denied!"); } if (e instanceof