With the new java filesystem utilities (1.7) the Path
relativize(Path)
method should work. From the Path Operations Tutorial.
Path p1 = Paths.get("joe");
Path p2 = Paths.get("sally");
In the absence of any other information, it is assumed that joe and sally are siblings, meaning nodes that reside at the same level in the tree structure. To navigate from joe to sally, you would expect to first navigate one level up to the parent node and then down to sally:
// Result is ../sally
Path p1_to_p2 = p1.relativize(p2);
// Result is ../joe
Path p2_to_p1 = p2.relativize(p1);
Now, whether such a technique is desirable, I'll leave that to others to comment.
Note that the paths do not need to exist on disk, and you can also declare a Path with a fixed root, so a Path
like new Path("/servlet/subdir/subdir2")
and a Path
like new Path("/servlet")
should relativize(...)
to a Path
like new Path("../..")
.`