在 NIO.2 中,创建软链接非常简单,只需要调用 Files.createSymbolicLink() 方法即可。这个方法需要传入软链接和目标文件的 Path 对象,并且可以传入一组文件属性,返回软链接的 Path 对象。
如果你的文件系统不支持软链接,那么将会抛出 UnsupportedOperationException 异常。记住,目标文件可以使用绝对路径也可以使用相对路径来定义,并且目标文件本身可以存在也可以不存在。
下面的代码将会简单创建一个拥有默认属性的软链接。软链接名为 rafael.nadal.1 目标文件文件为 C:\rafaelnadal\photos\rafa_winner.jpg:
…
Path link = FileSystems.getDefault().getPath("rafael.nadal.1");
Path target= FileSystems.getDefault().getPath("C:/rafaelnadal/photos", "rafa_winner.jpg");
try {
Files.createSymbolicLink(link, target);
} catch (IOException | UnsupportedOperationException | SecurityException e) {
if (e instanceof SecurityException) {
System.err.println("Permission denied!");
}
if (e instanceof UnsupportedOperationException) {
System.err.println("An unsupported operation was detected!");
}
if (e instanceof IOException) {
System.err.println("An I/O error occurred!");
}
System.err.println(e);
}
当你需要改变软链接的默认属性时,可以使用 createSymbolicLink 方法的第三个参数,这个参数的类型是 FileAttribute - 这个对象将封装一系列的文件属性供创建软链接的时候使用。下面的代码将演示如何从目标文件读取文件属性,并且将目标文件的文件属性设置到新创建的软链接上。新创建的软链接名为 rafael.nadal.2 目标文件是 C:\rafaelnadal\photos\rafa_winner.jpg:
…
Path link = FileSystems.getDefault().getPath("rafael.nadal.2");
Path target = FileSystems.getDefault().getPath("C:/rafaelnadal/photos", "rafa_winner.jpg");
try {
PosixFileAttributes attrs = Files.readAttributes(target, PosixFileAttributes.class);
FileAttribute<Set> 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 UnsupportedOperationException) {
System.err.println("An unsupported operation was detected!");
}
if (e instanceof IOException) {
System.err.println("An I/O error occured!");
}
System.err.println(e);
}
另外,也可以使用 setAttribute() 方法来更改文件属性。例如,下面的代码将读取目标文件的 lastModifiedTime 和 lastAccessTime 属性,并将这两个属性设置到新创建的软链接上。新创建的软链接名为 rafael.nadal.3,目标文件是 C:\rafaelnadal\photos\rafa_winner.jpg:
…
Path link = FileSystems.getDefault().getPath("rafael.nadal.3");
Path target = FileSystems.getDefault().getPath("C:/rafaelnadal/photos", "rafa_winner.jpg");
try {
Files.createSymbolicLink(link, target);
FileTime lm = (FileTime) Files.getAttribute(target,
"basic:lastModifiedTime", NOFOLLOW_LINKS);
FileTime la = (FileTime) Files.getAttribute(target,
"basic:lastAccessTime", NOFOLLOW_LINKS);
Files.setAttribute(link, "basic:lastModifiedTime", lm, NOFOLLOW_LINKS);
Files.setAttribute(link, "basic:lastAccessTime", la, NOFOLLOW_LINKS);
} catch (IOException | UnsupportedOperationException | SecurityException e) {
if (e instanceof SecurityException) {
System.err.println("Permision denied!");
}
if (e instanceof UnsupportedOperationException) {
System.err.println("An unsupported operation was detected!");
}
if (e instanceof IOException) {
System.err.println("An I/O error occured!");
}
System.err.println(e);
}
注:如果软链接已经存在,那么在创建软链接的时候会抛出 FileAlreadyExistsException 异常。
文章来源:
http://www.aptusource.org/2014/04/nio-2-creating-a-symbolic-link/
分享到:
相关推荐
Java I/O, NIO, and NIO.2 is a power-packed book that accelerates your mastery of Java's various I/O APIs. In this book, you'll learn about classic I/O APIs (File, RandomAccessFile, the stream classes ...
New I/O (NIO), and NIO.2 categories. You learn what each category offers in terms of its capabilities, and you also learn about concepts such as paths and Direct Memory Access. Chapters 2 through 5 ...
这是一本介绍java io以及nio相关知识的书,书中对知识的讲解通俗易懂,是学习java nio以及复习java io相关知识的必备书籍。注意:本书为英文版!!!
java nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava ...
Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf
蔚来-NIO.US-新车上市,蔚来可期.pdf
Apress.Pro.Java.7.NIO.2.2011
This book covers all the important aspects involved in developing NIO.2-based applications. It provides clear instructions for getting the most out of NIO.2 and offers many exercises and case studies ...
Pro Java 7 NIO.2 – PDF Books
Java NIO,Ron Hitchens 著,中文版 裴小星 译,Pro Java 7 NIO.2,Anghel Leonard 著,pdf文字版带书签,无安全限制
java NIO.zip
Pro Java 7 NIO.2.pdf,2011 by Anghel Leonard
This book covers all the important aspects involved in developing NIO.2-based applications. It provides clear instructions for getting the most out of NIO.2 and offers many exercises and case studies ...
JavaNIO.pdf
找了好久,终于找到了,java刷新同步获取网络资源
定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 NIO API 的集中抽象为: 缓冲区,它们是数据容器; 字符集 及其相关解码器 和编码器, 它们在字节和 Unicode 字符之间进行转换; 各种类型的通道,...
用java.nio.*进行网络编程
Java NIO.docx