• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getPath(), getAbsolutePath(), getCanonicalPath();??

 
Ranch Hand
Posts: 435
  • 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 getPath() and getAbsolutePath() and getCanonicalPath() methods?
What are Absolute pathnames and Relative pathnames?
:roll:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
To the best of my knowledge:
getPath() will return the absolute or relative path of the File object, depending on which it was constructed with.
getAbsolutePath() will return the current directory name concatenated with the file separator character and the pathname of the file
getCanonicalPath() will return a pathname with the relative references resolved.
Hope this is right, if not perhaps somebody else can set us both straight.
Cheers.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for example you're in /home/user directory in your unix system. if you want to go to /etc/passwd file, you can type either "vi /etc/passwd" which is absolute directory or "vi ../../etc/passwd" which is relative directory.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
File is one of the most deceiving classes in the API. It does not represent a file, it does not represent a directory. Remember that creating a File object does not imply you have got a corresponding file or directory in the file system.
A File object represents an abstract path name for a file or directory that may exist (or not) in the file system. Read the API for more details on that.
A File object can be thought as if were a string. What string? the one you pass to the constructor:
File(String)
or the one formed by concatenation of parent + File.separator + lastName in the constructor:
File(String parent, String lastName)
Let's tell that string the path name.
getPath() always return the path name. This can be
either absolute or relative, the same as we had passed the string arguments.
getParent() returns the part of the path name that is to the left of the last ocurrence of File.separator in the path name.
getName() returns the part of the path name that is to the right of the the last ocurrence of File.separator in the path name.
getAbsolutePath() returns the whole path name once it is made absolute (in case it were not)
Entries .. and . are left untouched and no IOException is launched if the path name is impossible under the file system naming convention.
getCanonicalPath() returns the path name, once it is made absolute, and once it is checked again the naming conventions of the file system: the entries .. and . resolved and an IOException is thrown if the path name is not possible (c:/c:/file.txt)
Note also that getParent sometimes won't return the
String parent argument.
I wrote a code some time ago:
 
reply
    Bookmark Topic Watch Topic
  • New Topic