我有java.nio.Path
指向绝对路径:
/home/user/project/resources/configuration.xml
我有第二个java.nio.Path
指向项目的根目录,也是绝对路径:
/home/user/project
现在是否可以创建一个java.nio.Path
来保存两者之间的相对路径:
resources/configuration.xml
答案 0 :(得分:8)
这正是relativize(Path)
方法的作用:
Path confFile = Paths.get("/home/user/project/resources/configuration.xml");
Path rootDir = Paths.get("/home/user/project");
Path relative = rootDir.relativize(confFile);