Using nio.relativize for a normalized path

Usually the path ignores everything .

(this directory) it contains. Thus, c:\\personal\\.\\photos\\readme.txt

and c:\\personal\\photos\\readme.txt

should give the same results for different operations, but in the following code, the normalized path gives a different result. Can anyone explain the reason for this?

Path p1 = Paths.get("c:\\personal\\.\\photos\\readme.txt"); 
Path p2 = Paths.get("c:\\personal\\index.html"); 
Path p3 = p1.relativize(p2); 
System.out.println(p3);

p1 = p1.normalize();
p2 = Paths.get("c:\\personal\\index.html"); 
p3 = p1.relativize(p2); 
System.out.println(p3);

      

Output:

..\..\..\index.html
..\..\index.html

      

+3


source to share


1 answer


By default, the path class does not ignore \\. ... This happens when you explicitly ask for normalize (). Here in oracle documentation on path relativization method

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html#relativize(java.nio.file.Path)


For example, if this path is "/ a / b" and the given path is "/ a / x", then the resulting relative path might be "../x".

So the answer might be that the default path does not drop the \\. ... Which together with the oracle documentation gives the result you see.

+1


source







All Articles