2017-11-25 117 views
4

我有一個java.nio.Path,它指向一個絕對路徑:爪哇 - 創建相對java.nio.Path從兩個java.nio.Path的

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

我有第二個java.nio.Path指向的根目錄項目,也是一個絕對路徑:

/home/user/project 

是現在可以創建一個java.nio.Path保持兩者之間的相對路徑:

resources/configuration.xml 
+1

類似的問題:https://stackoverflow.com/questions/204784/how-to-construct-a-relative-path-in-java-從二個絕對路徑 - 或網址 –

回答

6

這正是relativize(Path)方法做什麼:

Path confFile = Paths.get("/home/user/project/resources/configuration.xml"); 
Path rootDir = Paths.get("/home/user/project"); 
Path relative = rootDir.relativize(confFile); 
這裏