Uniqueness of a Directory Entry – Java I/O: Part II

Uniqueness of a Directory Entry

The method isSameFile() in the Files class can be used to check whether two paths denote the same directory entry. It does not take into consideration other aspects of the directory entry, like its file name or contents.

Click here to view code image

static boolean isSameFile(Path path1, Path path2) throws IOException

Determines whether the two paths denote the same directory entry. If the paths are equal, it returns true and does not check whether the paths exist.

This method normalizes the paths and follows symbolic links.

It implements an equivalence relation (which is reflexive, symmetric, and transitive) for non-null paths, if the file system and the directory entries do not change.

The numbers below refer to corresponding lines in the code to illustrate the workings of the isSameFile() method:

(1) Paths are always normalized, as in the case of path2.

(2) Symbolic links are always followed, as in the case of symbLink.

(3) Only paths are compared. Paths passed to the method are not equal.

(4) Equal paths return true, and their existence is not checked. The path ./Main.java does not exist.

(5) The paths must exist in the file system, if they are not equal. The path ./Main.java does not exist, resulting in a java.nio.file.NoSuchFileException.

Click here to view code image

Path path1 = Path.of(“project”, “src”, “pkg”, “Main.java”);
Path path2 = Path.of(“project”, “..”, “project”, “.”, “src”, “pkg”, “Main.java”);
Path target   = Path.of(“project”, “src”, “manifest.txt”);
Path symbLink = Path.of(“project”, “manifest_link”);
System.out.println(Files.isSameFile(path1, path2));            // (1) true
System.out.println(Files.isSameFile(symbLink, target));        // (2) true
System.out.println(Files.isSameFile(path1, target));           // (3) false
System.out.println(Files.isSameFile(Path.of(“Main.java”),
                                    Path.of(“Main.java”)));    // (4) true
System.out.println(Files.isSameFile(path1,
                           Path.of(“Main.java”)));  // (5) NoSuchFileException

Deleting Directory Entries

The methods delete() and deleteIfExists() in the Files class can be used for deleting directory entries.

Click here to view code image

static void delete(Path path) throws IOException
static boolean deleteIfExists(Path path) throws IOException

Delete a directory entry denoted by the path.

If the path does not exist, the first method throws a NoSuchFileException, but the second method does not.

Deleting a symbolic link only deletes the link, and not the target of the link.

To delete a directory, it must be empty or a java.nio.file.DirectoryNotEmpty-Exception is thrown.

Consider the following paths that exist:

Click here to view code image

Path projDir  = Path.of(“project”);
Path target   = Path.of(“project”, “src”, “manifest.txt”);
Path symbLink = Path.of(“project”, “manifest_link”);

The delete() method throws a NoSuchFileException, if the path does not exist.

Click here to view code image

Files.delete(symbLink);                 // Exists. Link deleted, not target.
Files.delete(Path.of(“Main.java”));     // Does not exist: NoSuchFileException

The deleteIfExists() method does not throw a NoSuchFileException. It indicates the result by the boolean return value.

Click here to view code image

System.out.println(Files.deleteIfExists(target));               // Exists.
                                                                //  Deleted: true
System.out.println(Files.deleteIfExists(Path.of(“Main.java”))); // Does not
                                                                //   exist: false

In order to delete a directory, it must be empty. The directory ./project is not empty. Both methods throw a DirectoryNotEmptyException.

Click here to view code image

Files.delete(projDir);                             // DirectoryNotEmptyException
System.out.println(Files.deleteIfExists(projDir)); // DirectoryNotEmptyException

Also keep in mind that deleting a directory entry might not be possible, if another program is using it.

Categories: , ,

Leave a Reply

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