Java - 从两个java.nio.Path创建相对java.nio.Path

时间:2017-11-25 15:17:44

标签: java filepath

我有java.nio.Path指向绝对路径:

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

我有第二个java.nio.Path指向项目的根目录,也是绝对路径:

/home/user/project

现在是否可以创建一个java.nio.Path来保存两者之间的相对路径:

resources/configuration.xml

1 个答案:

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