Java - create relative java.nio.Path from two java.nio.Path's

Viewed 214

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
1 Answers

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