Converting a Path to an Absolute Path
The method toAbsolutePath() of the Path interface converts a path to an absolute path. Table 21.2 illustrates how this method works for Path objects declared in the second column. A print statement that calls the method on each path declaration, analogous to the one below, creates the text representation of the resulting absolute path shown in the rightmost column. The current directory has the absolute path /a/b.
System.out.println(absPath1.toAbsolutePath()); // (1) /a
In Table 21.2, (1) and (2) show that if the Path object already represents an absolute path, the same Path object is returned. If the Path object represents a relative path, as shown at (3), (4), and (5), the resulting absolute path is created by appending the relative path to the absolute path of the current directory. The path need not exist in the file system, and the method does not attempt to clean up the path string of the resulting Path object—in contrast to the normalize() method (p. 1299).
Table 21.2 Converting to an Absolute Path
Absolute path of the current directory: /a/b Path | Text representation of the absolute path returned by the toAbsolutePath() method | |
(1) | Path absPath1 = Path.of(“/a”); | /a |
(2) | Path absPath2 = Path.of(“/a/b/c”); | /a/b/c |
(3) | Path relPath1 = Path.of(“d”); | /a/b/d |
(4) | Path relPath2 = Path.of(“../f”); | /a/b/../f |
(5) | Path relPath3 = Path.of(“./../g”); | /a/b/./../g |
Normalizing a Path
The normalize() method of the Path interface removes redundancies in a Path object. For example, the current directory designator “.” is redundant in a Path object, as it does not add any new level to the path hierarchy. Also the string “dir/..” in a path is redundant, as it implies going one level down in the path hierarchy and then one level up again—not changing the hierarchy represented by the path.
Table 21.3 illustrates how the normalize() method converts the Path objects declared in the second column. A print statement that calls the method on each path declaration, analogous to the one below, creates the text representation of the resulting path shown in the rightmost column.
System.out.println(path1.normalize()); // (1) a/b/c
The numbers below refer to the rows in Table 21.3.
(1) All occurrences of the current directory designator “.” are redundant and are eliminated from the path.
(2) The occurrences of the redundant string “a/..” are eliminated from the path.
(3) The occurrences of the parent directory designator “..” are significant in this case, as each occurrence implies traversing one level up the path hierarchy.
(4) All occurrences of the current directory designator “.” are eliminated from the path, but occurrences of the parent directory designator “..” are significant.
(5) The occurrence of the redundant current directory designator “.” is eliminated from the path, resulting in an empty path whose text representation is the empty string.
Because of redundancies in a path, comparison on paths is best performed on normalized paths (p. 1303).
Table 21.3 Normalizing Paths
Path | Text representation of the path returned by the normalize() method | |
(1) | Path path1 = Path.of(“./a/./b/c/.”); | a/b/c |
(2) | Path path2 = Path.of(“a/../a/../b”); | b |
(3) | Path path3 = Path.of(“../../d”); | ../../d |
(4) | Path path4 = Path.of(“./../../.”); | ../.. |
(5) | Path path5 = Path.of(“.”); | empty string |
Leave a Reply