• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

difference between toAbsolutePath() and toRealPath()

 
Ranch Hand
Posts: 658
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between toAbsolutePath() and toRealPath() . Both returns exactlly the same path object .


both will give same output :
C:\io\newdir
C:\io\newdir
 
Ranch Hand
Posts: 385
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
toRealPath() will resolve symbolic links.

So if C:\io was a symbolic link to D:\my_io, then toRealPath() would give D:\my_io\newdir.

Also, if the path+file doesn't actually exist, then toRealPath() throws an exception whereas toAbsolutePath() doesn't.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ahmed
 
author
Posts: 4354
45
jQuery Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most important distinction is that toRealPath() operates on actual file system records, whereas toAbsolutePath() is based on theory. For example, you can create a dozen Path objects that's do not exist, perform numerous operations in them such as relativize(), toAbsolutePath(), normalize(), etc. Once you call toRealPath(), though, the application will throw an exception if the Path does not exist.
 
Ranch Hand
Posts: 460
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So,


My code don't throws any exception.

The path D://PrjJava//OCP71//..  don't exist.




My question is : Where is the mistake?

Thanks in advance.

 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the PrjJava directory exist?
If so then toRealPath() won't throw an exception for "D:/PrjJava/OCP71/..".
 
Dana Ucaed
Ranch Hand
Posts: 460
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally, I understand the behavior:




The last statement throws java.nio.file.NoSuchFileException: D:\PrjJava\OCP71.

and the second statement returns D:\PrjJava.


 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic