Page 1 of 1

About Question enthuware.ocpjp.v7.2.1596 :

Posted: Tue Sep 17, 2013 3:38 am
by The_Nick
Hi,
You say in the explanation quoting the javadoc:
Where this path and the given path do not have a root component, then a relative path can be constructed.

A relative path cannot be constructed if only one of the paths have a root component.

Where both paths have a root component then it is implementation dependent if a relative path can be constructed.
In your question your paths are:
 Path p1 = Paths.get("\\personal\\readme.txt");
Path p2 = Paths.get("\\index.html");
 Path p3 = p1.relativize(p2);
 System.out.println(p3);
Both p1 and p2 are starting with: "\\".
then quoting again from the explanation:
A relative path cannot be constructed if only one of the paths have a root component.
And it's true because if you remove "\\" from either of the 2 paths, it's going to throw an exception.

However, after it says:
Where both paths have a root component then it is implementation dependent if a relative path can be constructed.
In your question both have "\\" does it imply that it's implementation dependent?

Thanks in advance.

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

Posted: Tue Sep 17, 2013 3:42 am
by The_Nick
Another question related, I have tried the following:

Code: Select all

Path path = Paths.get("\\indeed\\try");
		Path path1 = Paths.get("\\home\\user\\Desktop\\Enthu\\try1.txt");
		System.out.println(path.relativize(path1));
I got this output: ../\home\user\Desktop\Enthu\try1.txt

What is ../?!?

Thanks in advance.
Sorry about all these questions, it's just that I have got the exam on Friday and I am getting a little "what if there is this question.. "

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

Posted: Tue Sep 17, 2013 6:33 am
by admin
These are relative paths on Windows because we are using \\ (which is basically \) and not /. \ is used on Windows while / is used on *nix.

Also, since there is no option that says there will be an error (or implementation dependent), you can assume that they are relative paths and the question is not trying to test the border case where both the paths have root component.

HTH,
Paul.

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

Posted: Tue Sep 17, 2013 9:50 am
by The_Nick
At the end of all "\\" is a root component or not? or only c:\\ is a root component.
A relative path can contain a root component, or only an absolute path can?
I think that there is a bit of confusion between path separator and root component.
Root component should be c: and path separator "\\".
Waiting to know what you think about it.
Thanks in advance.

The_Nick.

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

Posted: Tue Sep 17, 2013 10:54 am
by admin
Technically, yes, it is a root component. If you do getRoot() on p1, it should return "\".

So in that sense, yes, you are right, relativize would be implementation dependent
HTH,
Paul.

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

Posted: Wed Sep 18, 2013 5:02 am
by The_Nick
Moreover,

on WINDOWS:
Path path = Paths.get("photos\\vacation"); // windows
Path path1 = Paths.get("yellowstone");
Path path2 = path.relativize(path1);
System.out.print(path2);

output: ../yellowstone

on LINUX
Path path = Paths.get("photos/vacation"); // unix
Path path1 = Paths.get("yellowstone");
Path path2 = path.relativize(path1);
System.out.print(path2);

output: ../../yellowstone

Apparently even with two relative paths it's system dependent..

Which one is going to be used by Oracle windows or unix? I don't think is fair doing such things though, what about one prepares on unix and then finds out that at the exam one of the possible solution is ../yellowstone.. (windows) while actually in unix the corresponding result would be actually ../../yellowstone.

Thanks a lot in advance. I would hate to miss a point because of this inconsistency.

The_Nick.

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

Posted: Wed Sep 18, 2013 8:29 am
by admin
Are you sure you are getting

../yellowstone

on Windows? I just tried it and got

..\..\yellowstone

I don't think the output should be different on Unix and Windows in this case.

HTH,
Paul.

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

Posted: Wed Sep 18, 2013 8:55 am
by The_Nick
Yep you right, I have tried the code of windows on linux, hence "\\" was not considered a FileSeparator.

Thanks.

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

Posted: Wed Oct 29, 2014 3:30 pm
by jordanglassman
Why does

Code: Select all

index.html
"cancel" with a double dot? Doesn't this imply that

Code: Select all

index.html
is a directory?

In other words, these both return the same thing, but my guess was that filenames would be ignored in the calling path since you don't usually talk about a location relative to a file.

Code: Select all

        Path p1 = Paths.get("/personal/readme.txt");
        Path p2 = Paths.get("/index.html");
        Path p3 = p1.relativize(p2);
        System.out.println(p3);

        p1 = Paths.get("/personal/readme.txt/");
        p2 = Paths.get("/index.html/");
        p3 = p1.relativize(p2);
        System.out.println(p3);
Output:

Code: Select all

../../index.html
../../index.html

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

Posted: Fri Oct 31, 2014 7:00 am
by admin
No, the path doesn't really care whether it is a file or a directory. It is only when you try to open/use a file as a directory or viceversa that you get an exception.
In fact, you can actually create a directory named index.html!

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

Posted: Thu Aug 04, 2016 9:28 am
by insider
I suggest to explicitly add a couple more points to the explanation since this question has apparently caused confustion among test takers including me:
1) resolve(), relativize() and normalize() all operate on strings and don't check for actual file paths;
2) Path doesn't care whether segment is a file or a directory.
Because apparently people try to use their existing file system experience. It might cause confusion that the answer to go from "\personal\readme.txt" to "\index.html" is "..\..\index.html". The last segment is often perceived as a file thus it seems unnatural to ".." on it.

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

Posted: Thu Aug 04, 2016 10:42 pm
by admin
Added. Thank you for your feedback!
Paul.