NIO2 Path Symbols Java

44 Views Asked by At

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

There are 1 best solutions below

0
cyberbrain On

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");