5

I have a java.nio.Path which points to an absolute path:

/home/user/project/resources/configuration.xml

I have a second java.nio.Path which points to the root directory of the project, also an absolute path:

/home/user/project

Is it now possible to create a java.nio.Path which holds the relative path between the two:

resources/configuration.xml
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    Similar question here: https://stackoverflow.com/questions/204784/how-to-construct-a-relative-path-in-java-from-two-absolute-paths-or-urls – Alexandre Dupriez Nov 25 '17 at 15:23

1 Answers1

7

This is precisely what the relativize(Path) method does:

Path confFile = Paths.get("/home/user/project/resources/configuration.xml");
Path rootDir  = Paths.get("/home/user/project");
Path relative = rootDir.relativize(confFile);
Mureinik
  • 297,002
  • 52
  • 306
  • 350