27

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.

Community
  • 1
  • 1
user496949
  • 83,087
  • 147
  • 309
  • 426
  • See this answer: http://stackoverflow.com/a/1099370/985026 (Question asked was What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?) – MalcolmOcean Jul 15 '12 at 01:48

3 Answers3

37

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    Maybe a quick word about sym- and hardlinks would help too. – Voo Jul 15 '12 at 02:20
  • 1
    If your system supports hard-linking directories, there may be more than one canonical path. – Tom Hale Oct 16 '17 at 13:16
  • @TomHale - That's true; hadn't thought about that. However, there should still be only one canonical path for any particular path defined by a `File` object. – Ted Hopp Oct 16 '17 at 14:25
  • what do you think if I am developing an application for production and on production, UNIX based server is running should I use a canonical path or absolute path. Asking in terms of which will be best approach – Umar Tahir Sep 18 '20 at 07:17
  • 1
    @UmarTahir - It depends on why you need the path string. It often makes no difference to functionality; if it does, then of course use the one that is required for proper function. I believe (without having done the research to back this up) that `getCanonicalPath()` is a bit more expensive to evaluate than `getAbsolutePath()`. – Ted Hopp Sep 18 '20 at 12:31
10

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.

Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54
Aaron
  • 11,239
  • 18
  • 58
  • 73
0

There can be many absolute paths to a file. However there can only be one canonical path to a file. Read this

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
jamjam
  • 3,171
  • 7
  • 34
  • 39
  • 7
    Consider improving this answer to contain more than just a link. See http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – Rob Hruska Jul 15 '12 at 01:50