Resolving Two Paths – Java I/O: Part II

Resolving Two Paths

Table 21.4 illustrates how the resolve() method of the Path interface performs resolution between two paths. The four combinations of mixing absolute and relative paths when calling the resolve() method are represented by the rows (R1 and R2) and the columns (C1 and C2) in Table 21.4. The results shown are obtained by executing a print statement that calls the resolve() method, analogous to the one below, for each combination.

Click here to view code image

System.out.println(absPath1.resolve(absPath2));      // (R1, C1)

If the given path is an absolute path, it is returned as the result, as in column C1, regardless of whether the path on which the method is invoked is an absolute or a relative path. Otherwise, the method creates a result path by appending the given path to the path on which the method is invoked, as in column C2.

In the special case when the given path is an empty path, the method returns the path on which it was invoked:

Click here to view code image

Path anyPath = Path.of(“/a/n/y”);
Path emptyPath = Path.of(“”);
System.out.println(anyPath.resolve(emptyPath));       // /a/n/y

Note that the paths need not exist to use this method, and the resulting path after resolution is not normalized.

Table 21.4 Resolving Paths

p1.resolve(p2), where p1 can be absPath1 or relPath1, and where p2 can be absPath2 or relPath2C1C2
Path absPath2
  = Path.of(“/c”);

Path relPath2
  = Path.of(“../e/f”);

R1Path absPath1
  = Path.of(“/a/b”);

/c/a/b/../e/f
R2Path relPath1
  = Path.of(“d”);

/cd/../e/f
Constructing the Relative Path between Two Paths

Table 21.5 illustrates how the relativize() method of the Path interface constructs a relative path between this path and the given path, so that the resulting relative path denotes the same directory entry as the given path.

The four combinations of mixing absolute and relative paths when calling the relativize() method are represented by the rows (R1 and R2) and the columns (C1 and C2) in Table 21.5. The results shown are obtained by executing a print statement that calls the relativize() method, analogous to the one below, for each combination.

Click here to view code image

System.out.println(absPath1.revitalize(absPath2));     // (R1, C1)

For the case (R1, C1) where both paths are absolute paths in Table 21.5, the resulting relative path is constructed relative to the root of the directory hierarchy. The relative path between the absolute path /a/b and the given absolute path /c is ../../c, indicating traversing two levels up from the path /a/b to the root and joining with the given path /c. Both the resulting relative path ../../c and the given path /c denote the same directory entry.

For the case (R2, C2) where both paths are relative paths in Table 21.5, the resulting relative path is constructed relative to the current directory. The relative path between the relative path d and the given relative path e/f is ../e/f, indicating traversing one level up from the path d to the current directory and joining with the given path e/f. Both the resulting relative path ../e/f and the given path e/f denote the same directory entry.

From Table 21.5, we see that an IllegalArgumentException is thrown when both paths are neither absolute paths nor relative paths, as it is not possible to create a relative path between paths that do not satisfy this criteria.

The code below shows the relationship between the relativize() and the resolve() methods:

Click here to view code image

Path p = Path.of(“/a/b”);
Path other = Path.of(“/a/b/c/d”);
Path q = p.relativize(other);                               // c/d
System.out.println(p.relativize(p.resolve(q)).equals(q));   // true
System.out.println(p.resolve(q).equals(other));             // true

Note that the paths need not exist to use this method, as it operates syntactically on the path strings.

Table 21.5 Constructing a Relative Path between Two Paths

p1.relativize(p2), where p1 can be absPath1 or relPath1, and where p2 can be absPath2 or relPath2C1C2
Path absPath2
  = Path.of(“/c”);

Path relPath2
  = Path.of(“e/f”);

R1Path absPath1
  = Path.of(“/a/b”);

../../cIllegalArgumentException
R2Path relPath1
  = Path.of(“d”);

IllegalArgumentException../e/f
Categories: , ,

Leave a Reply

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