If I created a two paths such as:
Path path3 = Paths.get("E:\\data");
Path path4 = Paths.get("E:\\user\\home");
And then make a new Path(relativePath) by using the relativize() method on the two paths, creating: "..\user\home" does the path symbol(..) in this case refer to "data" or does it just indicate a relative path?
Path relativePath = path3.relativize(path4);
// ..\user\home <- output
So my Question is, what does the Path symbol (..) represent?
1 Answer 1
The relativize method needs two inputs but doesn't "secretly" encode the base path into it's output, so your relativePath has to be applied to another base path to actually access a path on disk.
But you can apply it to a different base path, e.g. if you want to sync two folder structures below two different base paths.
tl;dr: it just indicates a relative path.
But take care with your path separator: if you hardcode that into your path strings like in your example, it will fail on other systems. Better split up the individual parts in extra strings like this:
Path path4 = Paths.get("E:", "user", "home");
relativize()on the two paths and created"..\user\home"? What't the output if you usetoAbsolutePath().toString()on the resulting path?Path.relativize(Path other)(link) shows an example: if this path is "/a/b" and the given path is "/a/x" then the resulting relative path may be "../x". The relative path from "a/b" to "/a/x" is to go back to the parent folder ("..") and there to to go into the the target folder "x".