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

Jester Daro :

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 :

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=473391&siteId=1
Recommended