What is the difference between getPath() and getAbsolutePath() and getCanonicalPath() methods? What are Absolute pathnames and Relative pathnames? :roll:
Joined: Jan 11, 2002
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.
Joined: Dec 16, 2001
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.
Joined: Jul 03, 2001
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: