Possible Duplicate:
What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?
Any difference between those two?
canonicalpath and absolutepath?
If having difference, a real world example will be needed.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The difference is that there is only one canonical path to a file[1], while there can be many absolute paths to a file (depending on the system). For instance, on a Unix system,
/usr/local/../binis the same as/usr/bin.getCanonicalPath()resolves those ambiguities and returns the (unique) canonical path. So if the current directory was/usr/local, then:would print:
Per Voo’s suggestion: on Unix systems,
getCanonicalPath()will also resolve symbolic links if the symbolic link exists. Hard links are treated like normal files (which is basically what they are). Note, however, that a file need not exist for these methods to succeed.[1] Well, not quite. As @Tom Hale points out in a comment, if the file system supports hard-linked directories, there may be multiple canonical paths to a given file.