Java Newbie question : what is the different between getAbsolutePath() and getcanonicalPath() in file class. I can't get the meaning from the documents. in below code, their output are the same.
public class copyFile {
public static void main(String[] args) throws IOException {
File inputFile = new File("/home/kit.ho/");
System.out.println("get AbsolutePath");
System.out.println(inputFile.getAbsolutePath());
System.out.println("get CanonicalPath");
System.out.println(inputFile.getCanonicalPath());
}
}
getAbsolutePath() gets the path string after resolving it against the current directory if it's relative, resulting in a fully qualified path. getCanonicalPath() gets the path string after resolving any relative path against current directory, and removes any relative pathing ( .
The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object. If the pathname of the file object is absolute then it simply returns the path of the current file object. For Example: if we create a file object using the path as “program.
The getCanonicalPath() method is a part of Path class. This function returns the Canonical pathname of the given file object. If the pathname of the file object is Canonical then it simply returns the path of the current file object. The Canonical path is always absolute and unique, the function removes the '.
The getPath() method is a part of File class. This function returns the path of the given file object. The function returns a string object which contains the path of the given file object.
Suppose /home
was actually a symbolic link to /usr/home
. Then getAbsolutePath
would still return /home/kit.ho/
whereas getCanonicalPath
would resolve the symlink and return /usr/home/kit.ho/
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With