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
9931 gold badge10 silver badges21 bronze badges
asked Sep 13, 2022 at 13:14
3
  • 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? Commented Sep 13, 2022 at 13:22
  • 2
    Does this answer your question? What do the dots mean in relative file path? Commented Sep 13, 2022 at 13:44
  • The JavaDoc of 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". Commented Sep 13, 2022 at 21:37

1 Answer 1

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");
answered Sep 13, 2022 at 13:26
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.