The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
As mentioned previously, the 如前所述,java.nio.file
package, and the Path
class in particular, is "link aware." java.nio.file
包,特别是Path
类,是“链接感知”的。Every 每个Path
method either detects what to do when a symbolic link is encountered, or it provides an option enabling you to configure the behavior when a symbolic link is encountered.Path
方法要么检测遇到符号链接时要执行的操作,要么提供一个选项,使您能够配置遇到符号链接时的行为。
The discussion so far has been about symbolic or soft links, but some file systems also support hard links. 到目前为止的讨论都是关于符号链接或软链接的,但是一些文件系统也支持硬链接。Hard links are more restrictive than symbolic links, as follows:硬链接比符号链接更具限制性,如下所示:
Because of these restrictions, hard links are not used as often as symbolic links, but the 由于这些限制,硬链接不像符号链接那样经常使用,但是Path
methods work seamlessly with hard links.Path
方法可以无缝地与硬链接配合使用。
Several methods deal specifically with links and are covered in the following sections:有几种方法专门处理链接,并在以下章节中介绍:
If your file system supports it, you can create a symbolic link by using the 如果您的文件系统支持,则可以使用createSymbolicLink(Path, Path, FileAttribute<?>)
method. createSymbolicLink(Path, Path, FileAttribute<?>)
方法创建符号链接。The second 第二个Path
argument represents the target file or directory and might or might not exist. Path
参数表示目标文件或目录,可能存在,也可能不存在。The following code snippet creates a symbolic link with default permissions:以下代码段使用默认权限创建符号链接:
Path newLink = ...; Path target = ...; try { Files.createSymbolicLink(newLink, target); } catch (IOException x) { System.err.println(x); } catch (UnsupportedOperationException x) { // Some file systems do not support symbolic links. System.err.println(x); }
The FileAttributes
vararg enables you to specify initial file attributes that are set atomically when the link is created. FileAttributes
vararg允许您指定在创建链接时以原子方式设置的初始文件属性。However, this argument is intended for future use and is not currently implemented.然而,这一论点是为了将来使用,目前尚未实施。
You can create a hard (or regular) link to an existing file by using the 可以使用createLink(Path, Path)
method. createLink(Path, Path)
方法创建指向现有文件的硬链接(或常规链接)。The second 第二个Path
argument locates the existing file, and it must exist or a NoSuchFileException
is thrown. Path
参数定位现有文件,该文件必须存在,否则将引发NoSuchFileException
。The following code snippet shows how to create a link:以下代码段显示了如何创建链接:
Path newLink = ...; Path existingFile = ...; try { Files.createLink(newLink, existingFile); } catch (IOException x) { System.err.println(x); } catch (UnsupportedOperationException x) { // Some file systems do not // support adding an existing // file to a directory. System.err.println(x); }
To determine whether a 要确定Path
instance is a symbolic link, you can use the isSymbolicLink(Path)
method. Path
实例是否为符号链接,可以使用isSymbolicLink(Path)
方法。The following code snippet shows how:以下代码段显示了如何:
Path file = ...; boolean isSymbolicLink = Files.isSymbolicLink(file);
For more information, see Managing Metadata.有关更多信息,请参阅管理元数据。
You can obtain the target of a symbolic link by using the 您可以使用readSymbolicLink(Path)
method, as follows:readSymbolicLink(Path)
方法获得符号链接的目标,如下所示:
Path link = ...; try { System.out.format("Target of link" + " '%s' is '%s'%n", link, Files.readSymbolicLink(link)); } catch (IOException x) { System.err.println(x); }
If the 如果Path
is not a symbolic link, this method throws a NotLinkException
.Path
不是符号链接,此方法将抛出NotLinkException
。