File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes getPath(), getAbsolutePath(), getCanonicalPath();?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "getPath(), getAbsolutePath(), getCanonicalPath();??" Watch "getPath(), getAbsolutePath(), getCanonicalPath();??" New topic

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

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
What is the difference between getPath() and getAbsolutePath() and getCanonicalPath() methods?
What are Absolute pathnames and Relative pathnames?
kem bro

Joined: Jan 11, 2002
Posts: 1
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.
patrick tang
Ranch Hand

Joined: Dec 16, 2001
Posts: 44
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.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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:
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:

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: getPath(), getAbsolutePath(), getCanonicalPath();??
It's not a secret anymore!