操作路徑
加入兩條道路
可以使用 resolve() 方法連線路徑。傳遞的路徑必須是部分路徑,這是不包含根元素的路徑。
Path p5 = Paths.get("/home/");
Path p6 = Paths.get("arthur/files");
Path joined = p5.resolve(p6);
Path otherJoined = p5.resolve("ford/files");
joined.toString() == "/home/arthur/files"
otherJoined.toString() == "/home/ford/files"
規範化路徑
路徑可能包含元素 .(指向你當前所在的目錄)和 ..(指向父目錄)。
當在路徑中使用時,可以隨時移除 . 而不改變路徑的目的地,並且可以將 .. 與前面的元素一起移除。
使用 Paths API,可以使用 .normalize() 方法完成:
Path p7 = Paths.get("/home/./arthur/../ford/files");
Path p8 = Paths.get("C:\\Users\\.\\..\\Program Files");
p7.normalize().toString() == "/home/ford/files"
p8.normalize().toString() == "C:\\Program Files"