Creating Symbolic Links – Java I/O: Part II

Creating Symbolic Links

The code below illustrates creating symbolic links to directory entries. We assume that the symbolic link at (1) below does not exist; otherwise, a resounding File-AlreadyExistsException will be thrown by the createSymbolicLink() method of the Files class. However, the target path need not exists, but that might limit the file operations that can be performed using the symbolic link. We assume that the target path at (2) exists when the code below is run.

The createSymbolicLink() method can be called with a relative path or the absolute path of the target, as shown at (3a) and (3b), respectively. In both cases, the symbolic link will be created with the default file permissions, as we have not specified the optional FileAttribute variable arity parameter. Note that the symbolic link is created to a file or a directory, depending on the kind of the target.

The method readSymbolicLink() method of the Files class returns the path of the target denoted by the symbolic link.

Click here to view code image

try {
  Path symbLinkPath  = Path.of(“.”, “readme_link”);                     // (1)
  Path targetPath    = Path.of(“.”, “project”, “backup”, “readme.txt”); // (2)
  Path symbLink = Files.createSymbolicLink(symbLinkPath, targetPath);   // (3a)
//Path symbLink = Files.createSymbolicLink(symbLinkPath,
//                                         targetPath.toAbsolutePath());// (3b)
  Path target = Files.readSymbolicLink(symbLink);                       // (4)
  FileUtils.printDirEntryInfo(symbLink);
  FileUtils.printDirEntryInfo(target);
} catch (FileAlreadyExistsException fe) {
  fe.printStackTrace();
} catch (IOException ioe) {
  ioe.printStackTrace();
}

Possible output from the code:

Click here to view code image

Symbolic link: ./readme_link
rw-r–r–
File: ./project/backup/readme.txt
rw-r–r–

Creating Regular and Temporary Directories

The Files class provides methods to create regular and temporary directories.

Click here to view code image

static Path createDirectory(Path dir, FileAttribute<?>… attrs)
                            throws IOException

Creates a new directory denoted by the Path object. It does not create nonexistent parent directories, and throws a NoSuchFileException if any parent directory does not exist. It also throws a FileAlreadyExistsException if the directory entry with that name already exists.

Click here to view code image

static Path createDirectories(Path dir, FileAttribute<?>… attrs)
            throws IOException

Creates a directory by creating all nonexistent parent directories first. It does not throw an exception if any of the directories already exist. If the method fails, some directories may have been created.

Click here to view code image

static Path createTempDirectory(String prefix, FileAttribute<?>… attrs)
                                throws IOException
static Path createTempDirectory(Path dir, String prefix,
                                FileAttribute<?>… attrs)
                                throws IOException

Create a new directory in the default temporary-file directory or in the specified directory denoted by the Path object, using the mandatory non-null prefix to append to its generated name. A NoSuchFileException is thrown by the second method if the specified location does not exist.

For creating directories, we will use the following FileAttribute object denoted by the dirFileAttr reference (p. 1325). It specifies read, write, and execute permissions for all users: the owner, the group, and others—often called full permissions.

Click here to view code image

Set<PosixFilePermission> dPerms = PosixFilePermissions.fromString(“rwxrwxrwx”);
FileAttribute<Set<PosixFilePermission>> dirFileAttr =
        PosixFilePermissions.asFileAttribute(dPerms);

Categories: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *