• Post Reply Bookmark Topic Watch Topic
  • New Topic

Paths relativize understanding issues with .. and .

 
michael ference
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:

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

I am not able to understand why the answer would be ..\..\..\..\index.html

According to my understanding the relativize method is how to get from path 1 to path 2 and if broken down into a file tree we would have:

C:\
     | - personal
          | - photos
               | - neadme.txt
     | - personal
          | - index.html

so in order to get from neadme to index you would have to go up to personal then back down to index, but there is already a .. in p1 so wouldn't that maybe put neadme in personal already?  So where do all the extra ..'s come from in the answer?  Does it have to do with p1 having . and .. in it and if so I'm not sure how and why it affects things the way it does.  Thanks for the help!
 
Tony Docherty
Saloon Keeper
Posts: 3155
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because path p1 contains a "..", neadme.txt is in the personal directory and not the photos directory.

I'm not sure why you are getting the results you are but maybe relativize() requires that the paths are normalized. It works as expected if you call the normalize() method on each Path before calling relativize().
 
michael ference
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the correct answer to a question on a practice test that I'm taking and I just don't get it and can't find examples of relativize that explain this answer correctly.  It is also the answer I get when I compile this code fragment. 
 
Tony Docherty
Saloon Keeper
Posts: 3155
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you pass non normalised paths in then maybe the relativize() method just steps backwards/forwards through the parts of the paths to get from one to the other without trying to take into effect the meaning of "." and "..".

I this is how it works then going from p1 to p2 requires you step back from "neadme.txt" (add first ../), step back through ".." (add second ../), step back through "photos" (add third ../), step back through "." (add fourth ../), step forward to "index.html" (add index.html)
 
Henry Wong
author
Sheriff
Posts: 22641
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the method simply do not understand the special meaning of "." and "..".  In other words, four directories were specified, so it needs to undo those directories.

Henry
 
michael ference
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, that was incredibly helpful
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!