0

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?

happy songs
  • 835
  • 8
  • 21
  • How exactly have you used `relativize()` on the two paths and created `"..\user\home"`? What't the output if you use `toAbsolutePath().toString()` on the resulting path? – deHaar Sep 13 '22 at 13:22
  • 2
    Does this answer your question? [What do the dots mean in relative file path?](https://stackoverflow.com/questions/12066435/what-do-the-dots-mean-in-relative-file-path) – Progman Sep 13 '22 at 13:44
  • The JavaDoc of `Path.relativize(Path other)` ([link](https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html)) 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". – LuCio Sep 13 '22 at 21:37

1 Answers1

0

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");
cyberbrain
  • 3,433
  • 1
  • 12
  • 22