Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the different between getAbsolutePath and getCanonicalPath [duplicate]

Tags:

java

file

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());
    }
}
like image 637
TheOneTeam Avatar asked Sep 08 '11 10:09

TheOneTeam


People also ask

What is the difference between getAbsolutePath and 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 ( .

What is getAbsolutePath?

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.

What is getCanonicalPath?

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 '.

What does getPath return?

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.


1 Answers

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/.

like image 131
Jon Skeet Avatar answered Sep 28 '22 17:09

Jon Skeet