About Question enthuware.ocpjp.v8.2.1584 :
Moderator: admin
-
- Posts: 11
- Joined: Mon Jan 18, 2016 4:35 pm
- Contact:
About Question enthuware.ocpjp.v8.2.1584 :
I somehow dont seem to get this
c:\\personal\\.\\photos\\..\\readme.txt
Is the same as
c:\\personal\readme.txt ( The .. after photos should take you to personal ).
and relative to c:\\personal\\index.html , it is actually in the same directory. I am looking at it if I was doing a cd with the paths. Why does it evaluate to ..\..\..\..\index.html
I somehow to seem to have a block getting this.
c:\\personal\\.\\photos\\..\\readme.txt
Is the same as
c:\\personal\readme.txt ( The .. after photos should take you to personal ).
and relative to c:\\personal\\index.html , it is actually in the same directory. I am looking at it if I was doing a cd with the paths. Why does it evaluate to ..\..\..\..\index.html
I somehow to seem to have a block getting this.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Did you read the detailed explanation? Please let me know which part is causing you trouble so that we can explain further.
Paul.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 11
- Joined: Mon Jan 18, 2016 4:35 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
I understand the explanation and I dont dispute that is how it works. I am just mentioning the fact that when you actually change directories using a command prompt, the result is different.Does that make sense ?
c:\\personal\\.\\photos\\..\\readme.txt
cd c:\\personal\\.\\photos\\..\\
now Iam in c:\personal with a file readme.txt
c:\\personal\\index.html is in the same directory.
c:\\personal\\.\\photos\\..\\readme.txt
cd c:\\personal\\.\\photos\\..\\
now Iam in c:\personal with a file readme.txt
c:\\personal\\index.html is in the same directory.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
I am still not sure what is it that you are confused about but here are some thoughts anyway:
When you do this on the command line, the command line actually resolves the whole thing then and there. In other words, when you do cd c:\\personal\\.\\photos\\..\\, the command lines resolves the dots and dot dots and takes you to c:\personal.
The Java File object doesn't do any resolution, if you create a File object with cd c:\\personal\\.\\photos\\..\\, it remains as it is. i.e. it keeps the dots and dot dots intact inside the File object.
When you do this on the command line, the command line actually resolves the whole thing then and there. In other words, when you do cd c:\\personal\\.\\photos\\..\\, the command lines resolves the dots and dot dots and takes you to c:\personal.
The Java File object doesn't do any resolution, if you create a File object with cd c:\\personal\\.\\photos\\..\\, it remains as it is. i.e. it keeps the dots and dot dots intact inside the File object.
If you like our products and services, please help us by posting your review here.
-
- Posts: 11
- Joined: Mon Jan 18, 2016 4:35 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Thanks for the explanation.
-
- Posts: 31
- Joined: Wed Mar 16, 2016 8:38 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Hi all,
I didn't understand this step:
=>c:\\personal\\.\\photos\\.. + ..\..\..\index.html =>c:\\personal\\.\\photos + ..\..\index.html
I would have expected:
=>c:\\personal\\.\\photos\\.. + ..\..\..\index.html =>c:\\ + ..\..\index.html
instead.
Could you please explain?
Thank you very much
Bye
Fabio
I didn't understand this step:
=>c:\\personal\\.\\photos\\.. + ..\..\..\index.html =>c:\\personal\\.\\photos + ..\..\index.html
I would have expected:
=>c:\\personal\\.\\photos\\.. + ..\..\..\index.html =>c:\\ + ..\..\index.html
instead.
Could you please explain?
Thank you very much
Bye
Fabio
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Why do you think c:\\personal\\.\\photos\\.. will reduce to c:\\ ?
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Wed Mar 16, 2016 8:38 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Hallo,
because it is connected to ' ../../..'
because it is connected to ' ../../..'
-
- Posts: 31
- Joined: Wed Mar 16, 2016 8:38 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Hallo,
your response is anyway right...so it is as . or .. are considered as ein Directory .
The result is the same with:
public class Relativize
{
public static void main(String[] args) {
Path p1 = Paths.get("/personal/instead./photos/instead../readme.txt");
Path p2 = Paths.get("/personal/index.html");
Path p3 = p1.relativize(p2);
System.out.println(p3);
}
}
../../../../index.html
A little counterintuitive
Thank you
Fabio
your response is anyway right...so it is as . or .. are considered as ein Directory .
The result is the same with:
public class Relativize
{
public static void main(String[] args) {
Path p1 = Paths.get("/personal/instead./photos/instead../readme.txt");
Path p2 = Paths.get("/personal/index.html");
Path p3 = p1.relativize(p2);
System.out.println(p3);
}
}
../../../../index.html
A little counterintuitive
Thank you
Fabio
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
You wrote:
c:\\personal\\.\\photos\\.. resolves to c:\\
and
..\..\..\index.html resolves to ..\..\index.html
which, to me, doesn't make sense because there is only one dot dot in c:\\personal\\.\\photos\\.., and that will cancel out the path element just behind it, i.e. /photos. Therefore, c:\\personal\\.\\photos\\.. will shorten to c:\\personal\\.\\ and since dot is a reference to the same directory, it can be removed without affecting anything else and therefore that makes it c:\\personal\\ and not c:\\
This implies that you thinkI would have expected:
=>c:\\personal\\.\\photos\\.. + ..\..\..\index.html =>c:\\ + ..\..\index.html
c:\\personal\\.\\photos\\.. resolves to c:\\
and
..\..\..\index.html resolves to ..\..\index.html
which, to me, doesn't make sense because there is only one dot dot in c:\\personal\\.\\photos\\.., and that will cancel out the path element just behind it, i.e. /photos. Therefore, c:\\personal\\.\\photos\\.. will shorten to c:\\personal\\.\\ and since dot is a reference to the same directory, it can be removed without affecting anything else and therefore that makes it c:\\personal\\ and not c:\\
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Sun Jul 24, 2016 1:04 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Hi,
Could you help me make sense out of this:
I adapted the code to Linux environment, so "c:\\something" became "/something".
What's confusing me is why normalized and non-normalized versions produce different results.
Also, it seems to break the invariant:
Could you help me make sense out of this:
Code: Select all
scala> val p1 = Paths.get("/personal/./photos/../readme.txt")
p1: java.nio.file.Path = /personal/./photos/../readme.txt
scala> val p2 = Paths.get("/personal/index.html")
p2: java.nio.file.Path = /personal/index.html
scala> p1.relativize(p2)
res6: java.nio.file.Path = ../../../../index.html
scala> p1.normalize.relativize(p2)
res7: java.nio.file.Path = ../index.html
What's confusing me is why normalized and non-normalized versions produce different results.
Also, it seems to break the invariant:
Code: Select all
scala> p1.resolve(p1.relativize(p2)).normalize
res10: java.nio.file.Path = /index.html
scala> p1.resolve(p1.normalize.relativize(p2)).normalize
res11: java.nio.file.Path = /personal/index.html
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
p1 and p1.normalize contain different path elements. So why do you think relativize should return the same value?
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Wed Jun 14, 2017 1:56 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Hello.
I still do not understand something...
Javadoc says
The output:
So, p2 != p1.resolve(p3). Question N1: Why ?
Maybe it is because of
Or maybe it is related to JDK-6925169 (but i could not find description of this bug)?
tested on build 1.8.0_131-b11
I still do not understand something...
Javadoc says
Lets check:Relativization is the inverse of resolution. This method attempts to construct a relative path that when resolved against this path, yields a path that locates the same file as the given path.
Code: Select all
public static void main(String... strings) {
Path p1 = Paths.get("c:\\personal\\.\\photos\\..\\readme.txt");
Path p2 = Paths.get("c:\\personal\\index.html");
System.out.println("p1=\t" + p1);
System.out.println("p2=\t" + p2);
Path p3 = p1.relativize(p2);
System.out.println("p3=\t" + p3 + "\t/*this is a relative path, a result of p1.relativize(p2)*/");
System.out.println("p1.resolve(p3)=\t" + p1.resolve(p3));
System.out.println("(p1.resolve(p3)).normalize()=\t" + (p1.resolve(p3)).normalize());
}
Code: Select all
p1= c:\personal\.\photos\..\readme.txt
p2= c:\personal\index.html
p3= ..\..\..\..\index.html /*this is a relative path, a result of p1.relativize(p2)*/
p1.resolve(p3)= c:\personal\.\photos\..\readme.txt\..\..\..\..\index.html
(p1.resolve(p3)).normalize()= c:\index.html
Maybe it is because of
Both paths have root components, right? So, question N2: is the answer ('..\..\..\..\index.html') really correct to this puzzle?Where both paths have a root component then it is implementation dependent if a relative path can be constructed.
Or maybe it is related to JDK-6925169 (but i could not find description of this bug)?
tested on build 1.8.0_131-b11
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
On a windows system, a path starting with c:\\ is a path with root component. But not so on a *nix system.
So if you use paths with c:\\ and try p.relativize(p.resolve(q)).equals(q) on windows, it may not print true. But on *nix, it will print true.
HTH,
Paul.
So if you use paths with c:\\ and try p.relativize(p.resolve(q)).equals(q) on windows, it may not print true. But on *nix, it will print true.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Wed Jun 14, 2017 1:56 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
operation system is not mentioned in the question, although the paths look like windows paths.
So we have to think it does not matter where code is running.
- of course and everywhere, since according javadoc for this expression to be fulfilled the path q should not have a root component and both paths should be normalized.
it is not our case.
We have two absolute paths and one of them is not normalized.
Moreover, we can just remove prefix "c:\\" from the example of code above to make them not absolute. What do we get? The same answer but without prefix "c:\\" (tested on windows)
So my questions remain in force.
1) why p.resolve(p.relativize(q)).normilize() != q.normilize() ? Note, it is not the same as expression above.
I think it is an equivalent of what javadoc says: - it is the second line of method description.
Or I am not right and the expression and statement are not equivalent? Then how I can test this statement ('relativization is the inverse of resolution') ?
or maybe it is not true for two absolute windows-style paths where one of them has '..' inside (i.e. maybe it is mythical JDK-6925169)?
2) or maybe if it is not true since
What about linux? In the discussion above said that there is also problem here - instead '/personal/index.html' we get '/index.html'. I.e. almost the same answer,
So we have to think it does not matter where code is running.
So if you use paths with c:\\ and try p.relativize(p.resolve(q)).equals(q) on windows, it may not print true.
- of course and everywhere, since according javadoc for this expression to be fulfilled the path q should not have a root component and both paths should be normalized.
it is not our case.
We have two absolute paths and one of them is not normalized.
Moreover, we can just remove prefix "c:\\" from the example of code above to make them not absolute. What do we get? The same answer but without prefix "c:\\" (tested on windows)
So my questions remain in force.
1) why p.resolve(p.relativize(q)).normilize() != q.normilize() ? Note, it is not the same as expression above.
I think it is an equivalent of what javadoc says:
Code: Select all
Relativization is the inverse of {@link #resolve(Path) resolution}.
Or I am not right and the expression and statement are not equivalent? Then how I can test this statement ('relativization is the inverse of resolution') ?
or maybe it is not true for two absolute windows-style paths where one of them has '..' inside (i.e. maybe it is mythical JDK-6925169)?
2) or maybe if it is not true since
? If so, how we can calculate the true answer at all? It is unpredictable on windows.Where both paths have a root component then it is implementation dependent if a relative path can be constructed
What about linux? In the discussion above said that there is also problem here - instead '/personal/index.html' we get '/index.html'. I.e. almost the same answer,
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Don't use absolute path on windows for p and q and p.relativize(p.resolve(q)).equals(q) will print true.
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Wed Jun 14, 2017 1:56 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
what about p.resolve(p.relativize(q)).equals(q)?
so, p1.resolve(p3).equals(p2) should return true? for not absolute path? or for absolute also? or for windows? or everywhere?
By the way
output:
So, no. it will not print true for two not absolute paths if they have roots. but if no roots - yes, p.relativize(p.resolve(q)).equals(q) == true.
(this is from puzzle explanation)Observe that if you append this path to p1, you will get p2.
so, p1.resolve(p3).equals(p2) should return true? for not absolute path? or for absolute also? or for windows? or everywhere?
By the way
Lets check:Don't use absolute path on windows for p and q and p.relativize(p.resolve(q)).equals(q) will print true.
Code: Select all
public static void main(String... strings) {
String r = "\\"; // "c:\\"; // "";
Path p1 = Paths.get(r + "personal\\.\\photos\\..\\readme.txt");
Path p2 = Paths.get(r + "personal\\index.html");
System.out.println("p1=\t" + p1 + "; absolute=" + p1.isAbsolute() + "; root=" + p1.getRoot());
System.out.println("p2=\t" + p2 + "; absolute=" + p2.isAbsolute() + "; root=" + p2.getRoot());
Path p3 = p1.relativize(p2);
System.out.println("p3=\t" + p3 + "\t/*this is a relative path, a result of p1.relativize(p2)*/");
System.out.println("p1.resolve(p3).equals(p2) = " + p1.resolve(p3).equals(p2)); // output: false
System.out.println("p1.resolve(p1.relativize(p2)).equals(p2) = " + p1.resolve(p1.relativize(p2)).equals(p2)); // output: false
System.out.println("p1.relativize(p1.resolve(p2)).equals(p2) = " + p1.relativize(p1.resolve(p2)).equals(p2));
System.out.println("p1.resolve(p3)=\t" + p1.resolve(p3));
System.out.println("(p1.resolve(p3)).normalize()=\t" + (p1.resolve(p3)).normalize());
}
Code: Select all
p1= \personal\.\photos\..\readme.txt; absolute=false; root=\
p2= \personal\index.html; absolute=false; root=\
p3= ..\..\..\..\index.html /*this is a relative path, a result of p1.relativize(p2)*/
p1.resolve(p3).equals(p2) = false
p1.resolve(p1.relativize(p2)).equals(p2) = false
p1.relativize(p1.resolve(p2)).equals(p2) = false
p1.resolve(p3)= \personal\.\photos\..\readme.txt\..\..\..\..\index.html
(p1.resolve(p3)).normalize()= \index.html
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Sorry, I mistyped. I meant path with roots in my head but wrote absolute paths instead. Sorry about about that.
The following does print true:
Path p1 = Paths.get("personal\\.\\photos\\..\\readme.txt");
Path p2 = Paths.get("personal\\index.html");
System.out.println(p1.relativize(p1.resolve(p2)).equals(p2));
-Paul.
The following does print true:
Path p1 = Paths.get("personal\\.\\photos\\..\\readme.txt");
Path p2 = Paths.get("personal\\index.html");
System.out.println(p1.relativize(p1.resolve(p2)).equals(p2));
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Wed Jun 14, 2017 1:56 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
how to append path p3 to p1 to get p2? I mean programmatically, not like in explanation
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Code: Select all
Path p1 = Paths.get("personal\\.\\photos\\..\\readme.txt");
System.out.println("p1=" + p1.normalize());
Path p2 = Paths.get("personal\\index.html");
System.out.println("p2=" + p2.normalize());
Path p3 = p1.normalize().relativize(p2);
System.out.println("p3="+p3);
System.out.println("p="+p1.resolve(p3).normalize());
Code: Select all
p1=personal\readme.txt
p2=personal\index.html
p3=..\index.html
p=personal\index.html
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Wed Jun 14, 2017 1:56 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
in the line Path p3 = p1.normalize().relativize(p2);
p1.normalize() = ''personal\\readme.txt"
and the answer is "p3=..\index.html".
It is not correct. The correct is "p3=..\..\..\..\index.html"
and, it seems, the issue is in java
I do not know how much you can trust the following explanation, but people are talking about a bug in Path#relativize https://stackoverflow.com/questions/377 ... 2#37749932 (mysterious bug JDK-6925169)
p1.normalize() = ''personal\\readme.txt"
and the answer is "p3=..\index.html".
It is not correct. The correct is "p3=..\..\..\..\index.html"
Code: Select all
Path p1 = Paths.get("personal\\.\\photos\\..\\readme.txt");
Path p2 = Paths.get("personal\\index.html");
Path p3 = p1.relativize(p2);
System.out.println("p3=" + p3); // output: p3=..\..\..\..\index.html
System.out.println("p=" + p1.resolve(p3).normalize()); // output: p=..\..\index.html
I do not know how much you can trust the following explanation, but people are talking about a bug in Path#relativize https://stackoverflow.com/questions/377 ... 2#37749932 (mysterious bug JDK-6925169)
So, in my opinion, there is either a bug, or poor documentation. And the question is about edge case, with not correct explanation of answer.The Path implementation does not handle . and .. components correctly in relativize, but treats them like any other path component.
-
- Posts: 1
- Joined: Mon Feb 19, 2018 8:30 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
In java 9 I tested the code exactly:
And it prints
..\index.html
namely answer (c) which is what I would have expected to.
I'm guessing that the implementation changed between Java 8 and Java 9. I think perhaps a note should be added to this effect, so that it doesn't confused people.
Where I'm concerned the Java 9 implementation is very clearly the correct and proper one, and the Java 7/Java 8 are horrible bugs. Consider for example the following snippet of code:
In Java 9 it prints the reasonable:
Relative:
Resolved: c:\personal\.\photos
Normalized: c:\personal\photos
In Java 7 and Java 8 it prints the horrible and just plain wrong:
Relative: ..\..\photos
Resolved: c:\personal\.\photos\..\..\photos
Normalized: c:\photos
Code: Select all
Path p1 = Paths.get("c:\\personal\\.\\photos\\..\\readme.txt");
Path p2 = Paths.get("c:\\personal\\index.html");
Path p3 = p1.relativize(p2);
System.out.println(p3);
..\index.html
namely answer (c) which is what I would have expected to.
I'm guessing that the implementation changed between Java 8 and Java 9. I think perhaps a note should be added to this effect, so that it doesn't confused people.
Where I'm concerned the Java 9 implementation is very clearly the correct and proper one, and the Java 7/Java 8 are horrible bugs. Consider for example the following snippet of code:
Code: Select all
Path p1 = Paths.get("c:\\personal\\.\\photos\\");
Path p2 = Paths.get("c:\\personal\\photos");
Path p3 = p1.relativize(p2);
System.out.println("Relative: " + p3);
Path p4 = p1.resolve(p3);
System.out.println("Resolved: " + p4);
System.out.println("Normalized: " + p4.normalize());
Relative:
Resolved: c:\personal\.\photos
Normalized: c:\personal\photos
In Java 7 and Java 8 it prints the horrible and just plain wrong:
Relative: ..\..\photos
Resolved: c:\personal\.\photos\..\..\photos
Normalized: c:\photos
-
- Posts: 15
- Joined: Wed May 10, 2017 2:49 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Hi,
Can I understand the relativize() function's behavior like..
whatever the path elements('.' , '..' , 'file.txt' or 'dir') are included in the path object, the relativize considers it as a directory.
Therefore it relativize the path with '..'?
Regards,
Shamran.
Can I understand the relativize() function's behavior like..
whatever the path elements('.' , '..' , 'file.txt' or 'dir') are included in the path object, the relativize considers it as a directory.
Therefore it relativize the path with '..'?
Regards,
Shamran.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
Yes, that seems correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1584 :
p1.relativize(p2) => how to get to p2 from p1.
It might be worth pointing out that relativize DOES NOT normalize before working out the result, and consequently
p1.normalize().relativize(p2) will potentially give you a completely different result to p1.relativize(p2):
It might be worth pointing out that relativize DOES NOT normalize before working out the result, and consequently
p1.normalize().relativize(p2) will potentially give you a completely different result to p1.relativize(p2):
Code: Select all
Path p1 = Paths.get("c:\\personal\\.\\photos\\..\\readme.txt");
Path p2 = Paths.get("c:\\personal\\index.html");
Path p3 = p1.relativize(p2);
System.out.println(p3); //..\..\..\..\index.html <== actual answer
System.out.println(p1.normalize().relativize(p2)); //..\index.html <== what I thought would be the answer..
Who is online
Users browsing this forum: No registered users and 10 guests