Trail: Essential Classes
Lesson: Basic I/O
Section: File I/O (featuring NIO.2)
Links, Symbolic or Otherwise
Home Page > Essential Classes > Basic I/O
Links, Symbolic or Otherwise
As has been mentioned elsewhere in this lesson, the java.nio.file package, and the Path class in particular, is "link aware". Every Path method either knows what to do when a symbolic link is encountered or it provides an option allowing you to configure the behavior when a symbolic link is encountered.

All of the discussion so far has been about symbolic or soft links, but some file systems also support the notion of hard links. Hard links are more restrictive than symbolic links:

Because of these limitations, hard links are not used as often as symbolic links, but the Path methods work seamlessly with hard links.

There are several methods that deal specifically with links, and are covered in this section:

Creating a Symbolic Link

If your file system supports it, you can create a symbolic link using the createSymbolicLink(Path, FileAttribute<?>) method. The Path argument represents the target file or directory and may or may not exist. The following code snippet creates a symbolic link with default permissions:

Path newLink = ...;
Path target = ...;
try {
    newLink.createSymbolicLink(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 allows you to specify initial file attributes that are set atomically when the link is created. However, this 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 using the createLink(Path) method. The Path argument passed locates the existing file — and it must exist or a NoSuchFileException is thrown. The following code snippet shows how to create a link:

Path newLink = ...;
Path existingFile = ...;
try {
    newLink.createLink(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 method in the BasicFileAttributes class. The following code snippet shows how:

Path file = ...;
boolean isSymbolicLink = Attributes.readBasicFileAttributes(file, LinkOption.NOFOLLOW_LINKS).isSymbolicLink();
For more information on file attributes, see Managing Metadata.

Finding the Target of a Link

You can get the target of a symbolic link using the readSymbolicLink method:

Path link = ...;
try {
    System.out.format("Target of link '%s' is '%s'%n", link, link.readSymbolicLink());
} catch (IOException x) {
    System.err.println(x);
}

If the Path is not a symbolic link this method throws a NotLinkException.

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