Page 1 of 1
About Question enthuware.ocpjp.v7.2.1584 :
Posted: Wed Sep 18, 2013 11:41 am
by The_Nick
HI,
I haven't evaluated the test yet, however I chose ..\index.html as correct answer.
Considering though that both paths have root component and hence it's system implementation dependent.. could be
the one starting with c:\\ .. as well.
The_Nick
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Sat Oct 05, 2013 7:07 pm
by icepeanuts
If the code is like this: Path p3 = p1.normalize().relativize(p2);
the output will be ..\index.html. This is because normalize() removes all redundant name elements.
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Wed Jan 14, 2015 3:51 pm
by Svetopolk
I am also confused that p1.normalize().relativize(p2) ! = p1.relativize(p2)
I am just wonder what a practical use of this relative path: "..\..\..\..\index.html"
What purpose can I use it?
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Thu Jan 15, 2015 1:17 am
by admin
Relative paths are not just useful but necessary for portability of an application. Let's say an application (such as our ETS Viewer tool) needs to create files. The code doesn't know anything about the user's directory structure so where will it create a file without an absolute path? The code uses relative path, which is converted into an absolute path at runtime after getting the information from the user. More complicated applications may create a huge directory structure based on relative paths (which is where ../../../filename kind of paths come into picture), which are converted into absolute paths at run time using a base path.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Thu Jan 15, 2015 3:47 am
by Svetopolk
mmm. I guess you didn't get my question. Simple example:
Code: Select all
Path p1 = Paths.get("c:\\dir\\.\\.\\.\\.\\dir1");
Path p2 = Paths.get("c:\\dir\\dir2");
Path p3 = p1.relativize(p2);
System.out.println(p3);
Path p4 = p1.normalize().relativize(p2);
System.out.println(p4);
I have 2 paths and want to move some files from dir1 to dir2 using relative path. As you can see dir1 and dir2 are siblings. Assume our current location is dir1. Using p4 normalized path (..\dir2) I will achieve my goal but p3 path (..\..\..\..\..\dir2) is absolutely stupid and will move my files into root directory. So my conclusion is that using relativize without normalize is deprecated.
Correct usage is even worse:
Code: Select all
Path p4 = p1.normalize().relativize(p2.normalize())
Paul, What do you think about it?
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Thu Jan 15, 2015 4:49 am
by admin
Well, you can't expect one thing to work for every situation. So may be in your example, relative path doesn't make sense but that doesn't mean relative path is useless!
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Mon Jan 26, 2015 4:19 pm
by MicNeo
Hi,
I ran code from the question, and it printed: ../c:\personal\index.html
I choose answer: c:\personal\index.html which is wrong according to you, why? ..\..\..\..\index.html doesn't seems to be a right one. Can you recheck that?
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Tue Jan 27, 2015 12:02 am
by admin
Are you sure you ran the code exactly as shown? I just tried it and I got ..\..\..\..\index.html
-Paul.
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Tue Jan 27, 2015 3:14 am
by MicNeo
Yes, I copied it from etsviewer, here is online version of program I run:
http://goo.gl/kDC6bT
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Tue Jan 27, 2015 3:29 am
by admin
It could be because of the difference between *nix and windows. I tried on windows. c:\personal\index.htm is still wrong though because that is not what is printed on *nix.
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Tue Jan 27, 2015 3:51 am
by MicNeo
You're right, when I changed paths to unix way (like /personal/./photos/../readme.txt) the result is correct one. Huh, I have to review my knowledge about relativize() method then

I based on wrong exercises whole time
Thanks for help!
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Tue Jan 27, 2015 5:01 am
by MicNeo
Yes, it's because of that. I changed paths to unix way (like /personal/./photos/../readme.txt) and it returned expected result. Now that method have sense, all my exercises were giving strange output
Thanks for help.
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Mon Jan 25, 2016 2:12 pm
by toolforger
Chiming in with MicNeo.
It's logical actually. On any standard Unixoid file system, backslashes and colons are normal filename components. "c:\\personal\\.\\photos\\..\\readme.txt" then does not contain any directory separator, it's a normal filename.
@Enthuware: Please expand the explanation with this sentence:
"Note that the WindowsPath and UnixPath implementations for Path do not normalize their Path, i.e. the .. and . components in p1 are not eliminated and must be offset by a .. component from p2. This is unofficial though, the Path interface does not specify whether the implementation classes should normalize or not.
The same applies to Path#resolve: The Path interface does not say anything about normalization, so in theory and implementation class is free to normalize or not, in practice, all known implementations do not normalize."
(I'm pretty sure resolve does not normalize in UnixPath, code inspection of the call hierarchy revealed nothing related to normalization. I couldn't check WindowsPath.)
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Mon Jan 25, 2016 9:52 pm
by admin
Added.
thank you for your feedback!
Re: About Question enthuware.ocpjp.v7.2.1584 :
Posted: Tue Jan 26, 2016 2:22 am
by toolforger
Glad to help.
I'm pretty sure the wording can be improved though, I tend to write too long and too complicated sentences
