Get Absolute java.nio.Path

Is there a way to get the absolute java.nio.Path? I have a relative path path3

derived from Path path3 = path1.relativize(path2);

I want to get path2

from path1

and again path3

.

Will it path1.resolve(path3)

come back path2

?

For example : if path1 and path3 - then path2 contains path1.resolve(path3)


C:\Users\ABC\Documents\NetBeansProjects\JSF Sample\web


..\..\..\..\Pictures\BxqfOHfIIAApI.png


C:\Users\ABC\Documents\NetBeansProjects\JSF Sample\web\..\..\..\..\Pictures\BxqfOHfIIAApI.png

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

+3


source to share


1 answer


path1.resolve(path3)

will give you a path equivalent to, but not necessarily equal to, path2. You can do instead path1.resolve(path3).normalize()

.

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 the additional information in your edit, normalize () is exactly what you want.

+5


source







All Articles