Sameera Kumarasingha
Sameera Kumarasingha

Reputation: 2988

Using nio.relativize for a normalized path

Normally, a path ignores all the . (this directory) it contains. So, c:\\personal\\.\\photos\\readme.txt and c:\\personal\\photos\\readme.txt should give identical 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

Upvotes: 4

Views: 150

Answers (1)

smslce
smslce

Reputation: 416

Path class itself does not ignore \\. by default. It happens when you explicitly ask through normalize(). Here in oracle documentation on path's relativize 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 may be "../x".

So the answer might be that, path does not by default discards \\.. Which along with oracle documentation results the output you see.

Upvotes: 1

Related Questions