Documentation

The Java™ Tutorials
Hide TOC
Links, Symbolic or Otherwise链接,符号或其他
Trail: Essential Java Classes
Lesson: Basic I/O
Section: File I/O (Featuring NIO.2)

Links, Symbolic or Otherwise链接,符号或其他

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:有几种方法专门处理链接,并在以下章节中介绍:

Creating a Symbolic Link创建符号链接

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. FileAttributesvararg允许您指定在创建链接时以原子方式设置的初始文件属性。However, this argument is intended for future use and is not currently implemented.然而,这一论点是为了将来使用,目前尚未实施。

Creating a Hard Link创建硬链接

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参数定位现有文件,该文件必须存在,否则将引发NoSuchFileExceptionThe 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);
}

Detecting a Symbolic Link检测符号链接

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.有关更多信息,请参阅管理元数据

Finding the Target of a Link查找链接的目标

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


Previous page: Creating and Reading Directories
Next page: Walking the File Tree