16

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());
    }
}
TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
  • absoulet path gives the entire path for example /user/username/nameoffolder/txt while conical path is like nameoffolder/txt (notice there is no / before nameoffolder) hope that helps – Tushar Chutani Sep 09 '11 at 05:41
  • [Avajava](http://www.avajava.com/tutorials/lessons/whats-the-difference-between-a-files-path-absolute-path-and-canonical-path.html) has a fairly good explanation of the differences... – Costis Aivalis Sep 08 '11 at 10:48

1 Answers1

21

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Similar issue : http://stackoverflow.com/questions/1099300/whats-the-difference-between-getpath-getabsolutepath-and-getcanonicalpath – Santosh Sep 08 '11 at 10:55