About Question enthuware.ocpjp.v7.2.1584 :
Moderators: Site Manager, fjwalraven
-
- Posts: 132
- Joined: Thu May 16, 2013 9:23 am
- Contact:
About Question enthuware.ocpjp.v7.2.1584 :
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
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
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
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.
the output will be ..\index.html. This is because normalize() removes all redundant name elements.
-
- Posts: 22
- Joined: Sun Nov 18, 2012 1:51 am
- Location: Moscow
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
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?
I am just wonder what a practical use of this relative path: "..\..\..\..\index.html"
What purpose can I use it?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Sun Nov 18, 2012 1:51 am
- Location: Moscow
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
mmm. I guess you didn't get my question. Simple example:
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:
Paul, What do you think about it?
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);
Correct usage is even worse:
Code: Select all
Path p4 = p1.normalize().relativize(p2.normalize())
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
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.
-
- Posts: 5
- Joined: Tue May 27, 2014 1:09 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
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?
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?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
Are you sure you ran the code exactly as shown? I just tried it and I got ..\..\..\..\index.html
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Tue May 27, 2014 1:09 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
Yes, I copied it from etsviewer, here is online version of program I run: http://goo.gl/kDC6bT
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
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.
-
- Posts: 5
- Joined: Tue May 27, 2014 1:09 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
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!
Thanks for help!
-
- Posts: 5
- Joined: Tue May 27, 2014 1:09 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
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.
Thanks for help.
-
- Posts: 38
- Joined: Fri Nov 13, 2015 4:40 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
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.)
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.)
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
Added.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Fri Nov 13, 2015 4:40 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1584 :
Glad to help.
I'm pretty sure the wording can be improved though, I tend to write too long and too complicated sentences
I'm pretty sure the wording can be improved though, I tend to write too long and too complicated sentences
Who is online
Users browsing this forum: No registered users and 88 guests