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.
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.
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/../bin
is the same as /usr/bin
. getCanonicalPath()
resolves those ambiguities and returns the (unique) canonical path. So if the current directory was /usr/local
, then:
File file = new File("../bin");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
would print:
../bin
/usr/local/../bin
/usr/bin
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.
Here is a canonical path.
C:/files/foo.txt
Here are absolute paths
C:/files/FOO.TXT
C:/files/FOO.txt
C:/files/foo.TXT
C:/FILES/FOO.TXT
A canonical path is an absolute unique path to the file. A file can have only one canonical path and many absolute paths.
A file can have only have one canonical path which is the file path of the file. A file can have many absolute paths to the file.
There can be many absolute paths to a file. However there can only be one canonical path to a file. Read this