读取目标文件的属性并创建一个链接,将目标中的属性分配给该链接。
import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; /* *来 自 n o w j a v a . c o m - 时 代 Java */ import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.PosixFileAttributes; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.Set; public class Main { public static void main(String[] args) {//nowjava - 时 代 Java Path link = FileSystems.getDefault().getPath("test"); Path target = FileSystems.getDefault().getPath("C:/folder1/photos", "test.jpg"); try { PosixFileAttributes attrs = Files.readAttributes(target, PosixFileAttributes.class); FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions .asFileAttribute(attrs.permissions()); Files.createSymbolicLink(link, target, attr); } catch (IOException | UnsupportedOperationException | SecurityException e) { if (e instanceof SecurityException) { System.err.println("Permission denied!"); } if (e instanceof