• Post Reply Bookmark Topic Watch Topic
  • New Topic

Path.ToRealPath method  RSS feed

 
Pasquale Bonito
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I saw this test in Sierra/Bates study guide:

Having a directory x under c: drive what is the result of System.out.println(Paths.get("c:/x/z/..").toRealPath()); ?

I answered c:\x but according to the solution this is wrong. Correct solution should be: throws NoSuchFileException. The toRealPath() function actually looks at the filesystem. Since there is no directory named z an exception is thrown.

I thought that path was normalized removing directory z, but according the solution this should not be done.

However If i try
System.out.println(Paths.get("c:/users/BONPAS00/Documents/XXXXX/..").toRealPath());

the output is C:\Users\BONPAS00\Documents.

So I don't underdsand the exact behavior.

Thanks
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I tried it and got that Exception when there were tiny spelling errors in Paths. This is what it says in the Path class.
 
Pasquale Bonito
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked at javadoc as well and it's clear thatt .. removes previous dir. It seems that the exercise in Sierra/Bates is wrong.

thanks
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, Sierra and Bates appears to be correct. They said it would throw an Exception.
 
Pasquale Bonito
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is not about if that methods throws Exception or not but if it throws Exception in a specific case.

What happens if you create a dir "x" with no child under c:\ and than run a program with this line of code ? System.out.println(Paths.get("c:/x/z/..").toRealPath());
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it. I did. I suffered an Exception. I used .../references and all was well. I used .../References (which is wrong) and there was a NoSuchFileException. The Path#toRealPath method declares an IOException, however.
 
Pasquale Bonito
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it as well, and I didn't catch any exception since directory c:\x exists and path is normalized deleting ../y before looking at the real path.

I agree thta if the path does not exists on filesystem you hava an exceptio.
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it was the double dot in the book for parent directory and not a treble dot (=ellipsis=…)?

You are right that the double dot will remove the z directory and I hasn't noticed that, so I was mistaken there. Sorry.
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect such things may be platform‑dependent, but I tried the following paths on a Linux box:-
/home/campbell/Documents/lectures/Forth/References/../references
/home/campbell/Documents/lectures/Forth/references/../references

References does not exists, but references does. Tryint the first path even with .. throws an Exception, so it appears that .. only normalises a Path if whatever precedes the .. exists. Please check, but it still appears Sierra and Bates are correct there.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pasquale Bonito wrote:The question is not about if that methods throws Exception or not but if it throws Exception in a specific case.

What happens if you create a dir "x" with no child under c:\ and than run a program with this line of code ? System.out.println(Paths.get("c:/x/z/..").toRealPath());


Although the exact behavior of the Path methods will vary with the platform, referencing a non-existing element is almost guaranteed to fail.

The reason being that one of the things that getRealPath() will do unless otherwise instructed is resolve aliases.

So if I make a symlink /etc/tomcat/conf that equates to the real path /var/lib/apache-tomcat-6.0.28/conf, the result of the getRealPath("/etc/tomcat/conf/server.xml") would be "/var/lib/apache-tomcat-6.0.28/conf/server.xml".

Because of this potential, it's not safe to assume that "/etc/tomcat/conf/.." is really /etc/tomcat/conf if the "conf" directory doesn't exist at the moment.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!