Forums Register Login

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

+Pie Number of slices to send: Send
What is the difference between getPath() and getAbsolutePath() and getCanonicalPath() methods?
What are Absolute pathnames and Relative pathnames?
:roll:
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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:
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 9307 times.
Similar Threads
getPath() , getAbsolutePath() &getCanonicalPath()
getpath(),getAbsolutepath(),getCanonicalpath()
getAbsolutePath() and getRelativePath()
How to make it relative path?
File Class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:45:41.