简体   繁体   中英

Get Absolute java.nio.Path

Is there a way to get absolute java.nio.Path ? I have a relative path path3 retrieved from Path path3 = path1.relativize(path2); I want to get again path2 from path1 and path3 .

Will path1.resolve(path3) return path2 ?

For example path1.resolve(path3) :
if path1 is C:\\Users\\ABC\\Documents\\NetBeansProjects\\JSF Sample\\web
and path3 is ..\\..\\..\\..\\Pictures\\BxqfOHfIIAApI.png
then path2 contains C:\\Users\\ABC\\Documents\\NetBeansProjects\\JSF Sample\\web\\..\\..\\..\\..\\Pictures\\BxqfOHfIIAApI.png

How to get path2 like C:\\Users\\ABC\\Pictures\\BxqfOHfIIAApI.png

path1.resolve(path3) will get you a path that's equivalent but not necessarily equal to path2. You may want to do path1.resolve(path3).normalize() instead.

path1 = Paths.get("/var");
path2 = Paths.get("/tmp");

path3 = path1.relativize(path2);  // path3 is "../tmp"
path4 = path1.resolve(path3);     // path4 is "/var/../tmp"

path5 = path4.normalize();        // path5 is "/tmp"

Edit : Based on additional info in your edit, normalize() is exactly what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM