About Question enthuware.ocpjp.v7.2.1584 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

About Question enthuware.ocpjp.v7.2.1584 :

Post 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

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post 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.

Svetopolk
Posts: 22
Joined: Sun Nov 18, 2012 1:51 am
Location: Moscow
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post 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?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post 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.
If you like our products and services, please help us by posting your review here.

Svetopolk
Posts: 22
Joined: Sun Nov 18, 2012 1:51 am
Location: Moscow
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post 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?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post 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!
If you like our products and services, please help us by posting your review here.

MicNeo
Posts: 5
Joined: Tue May 27, 2014 1:09 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post 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?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post by admin »

Are you sure you ran the code exactly as shown? I just tried it and I got ..\..\..\..\index.html
-Paul.
If you like our products and services, please help us by posting your review here.

MicNeo
Posts: 5
Joined: Tue May 27, 2014 1:09 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post by MicNeo »

Yes, I copied it from etsviewer, here is online version of program I run: http://goo.gl/kDC6bT

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post 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.
If you like our products and services, please help us by posting your review here.

MicNeo
Posts: 5
Joined: Tue May 27, 2014 1:09 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post 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 :P

Thanks for help!

MicNeo
Posts: 5
Joined: Tue May 27, 2014 1:09 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post 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.

toolforger
Posts: 38
Joined: Fri Nov 13, 2015 4:40 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post 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.)

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post by admin »

Added.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

toolforger
Posts: 38
Joined: Fri Nov 13, 2015 4:40 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1584 :

Post 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 :-D

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests