Path p1 = Paths.get(\"/Users/jack/Documents/text1.txt\");
Path p2 = Paths.get(\"/Users/jack/text2.txt\");
Path result1 = p1.resolve(p2);
Path result2 = p1.relativize(p2)
If the other parameter is an absolute path then this method trivially returns other.
Path p1 = Paths.get("/usr");
Path p2 = Paths.get("/etc");
Path p3 = p1.resolve(p2); // returns /etc
If other is an empty path then this method trivially returns this path.
Path p1 = Paths.get("/usr");
Path p2 = Paths.get("");
Path p3 = p1.resolve(p2); // returns /usr
Otherwise this method considers this path to be a directory and resolves the given path against this path.
Path p1 = Paths.get("/usr");
Path p2 = Paths.get("local");
Path p3 = p1.resolve(p2); // returns /usr/local
Path p1 = Paths.get("/usr");
Path p2 = Paths.get("/usr/local");
Path p3 = p1.relativize(p2); // returns local
This also means that
Path p1 = Paths.get("/usr");
Path p2 = Paths.get("/usr/../usr/local");
Path p3 = p1.relativize(p2); // returns local
But if the two paths are equal returns an empty path
Path p1 = Paths.get("/usr");
Path p2 = Paths.get("/usr/../usr");
Path p3 = p1.relativize(p2); // returns empty path
resolve()
: Joins two paths.
relativize ()
: construct a path from one location in the file system to another location.
Output explanation:
result1: /Users/jack/text2.txt
: because you passed in an absolute path, resolve()
returns the passed in the path as is if it is an absolute path.
result2: ../../text2.txt
: to get to /Users/jack/text2.txt
from /Users/jack/Documents/text1.txt"
you need to to go two levels up, and then just select `text2.txt file.
These are the code snippets from my code base that help you to understand the use of the resolve() method
private File initUsersText() throws Exception
{
Path dir = testdir.getPath().toRealPath();
FS.ensureDirExists(dir.toFile());
File users = dir.resolve("users.txt").toFile();
writeUser( users );
return users;
}
private File initUsersText() throws Exception
{
Path dir = testdir.getPath().toRealPath();
FS.ensureDirExists(dir.toFile());
File users = dir.resolve("users.txt").toFile();
writeUser( users );
return users;
}
And these are the examples of the use of relativize()
public ScopePath pathToClassName(Path file) {
if (!isValidClass(file))
return null;
Path relativePath = root.relativize(root.resolve(file));
String withoutExtension = removeExtension(relativePath.toString());
return new ScopePath(withoutExtension.replace(File.separator, "."));
}
private String getRelativePath(Path p) {
String relativePath = packageDir.relativize(p)
.toString();
if (File.separator.equals("\\")) {
relativePath = relativePath.replace("\\", "/");
}
return relativePath;
}
I cannot understand how resolve() and relativize() method works?
Path resolve(Path) resolves the given path against this
path.
Path relativize(Path) constructs a relative path of the given path against this
path .
These are reverse operations.
Path resolve(Path other)
In the general use case of resolve()
, you want to return a new Path
object where you will join this Path
object to the Path
parameter that is a relative Path
such as :
Path p1 = Paths.get("/Users/jack");
Path p2 = Paths.get("text1.txt");
Path result1 = p1.resolve(p2);
Here result1
will be the path join of p1
and p2
, that is : /Users/jack/text1.txt
.
In your example the parameter passed to the method is not a relative Path
but an absolute :
Path p1 = Paths.get("/Users/jack/Documents/text1.txt");
Path p2 = Paths.get("/Users/jack/text2.txt");
Path result1 = p1.resolve(p2);
It makes no sense to "append" a Path
to another if the second is an absolute Path
.
So the javadoc considers that in this case the parameter is returned as result of resolve()
:
If the other parameter is an absolute path then this method trivially returns other.
Path relativize(Path other)
The doc says more specifically :
This method attempts to construct a relative path that when resolved against
this
path, yields a path that locates the same file as the given path.
It means that the returned Path
is the relative path of the Path
parameter relatively to this
Path
.
For example, if this
path is "/a/b"
and the given path is "/a/b/c/d"
then the resulting relative path would be "c/d"
.
We will check that with your example :
Path p1 = Paths.get("/Users/jack/Documents/text1.txt");
Path p2 = Paths.get("/Users/jack/text2.txt");
Path result2 = p1.relativize(p2);
// result2= ../../text2.txt
The ../../text2.txt
Path is expected as result as the relative path produced ( ../../text2.txt
) resolved against this
path (/Users/jack/Documents/text1.txt
) yields a path that locates the same file as the given path (/Users/jack/text2.txt
) :
Paths.of("/Users/jack/Documents/text1.txt").resolve("../../text2.txt")
returns -> /Users/jack/text2.txt
The resolve(Path)
is a method for creating a new Path by joining an existing path to the current path.
Path path1 = Paths.get("/test1/../test2");
Path path2 = Paths.get("test3");
System.out.println(path1.resolve(path2));
The result will be: /test1/../test2/test3
In fact, the method relativize(Path) is used for constructing the relative path from one Path object to another:
Path path1= Paths.get("E:\\test1");
Path path2= Paths.get("E:\\test2\\test3");
System.out.println(path1.relativize(path2));
System.out.println(path2.relativize(path1));
The result will be:
..\test2\test3 relative path from path1 to path2
..\..\test1 relative path from path2 to path1