Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Canonical vs Absolute

 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody tell me the difference between the getCanonicalPath() and getAbsolutePath() methods of the File class? I am reading the API once and once again and I can�t understand the diference. Thanks. (I made some code too)
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The definition of absolute pathname is system dependent. On UNIX systems, a pathname is absolute if its prefix is "/". On Win32 systems, a pathname is absolute if its prefix is a drive specifier followed by "\\", or if its prefix is "\\".
Canonical paths are a bit harder. For starters, all canonical paths are absolute (but not all absolute paths are canonical). A single file existing on a system can have many different paths that refer to it, but only one canonical path.
Try the following program:
<code><pre>
import java.io.*;

public class Test {
public static void showPaths(String pathName) throws IOException {
File file = new File(pathName);
System.out.println("path: " + file.getPath());
System.out.println("absolute path: " + file.getAbsolutePath());
System.out.println("canonical path: " + file.getCanonicalPath());
System.out.println();
}

public static void main(String[] s) throws IOException {
File file = new File(new File("test.txt").getAbsolutePath());
String parent = file.getParent();
File parentFile = new File(parent);
String parentName = parentFile.getName();
String grandparent = parentFile.getParent();
file.createNewFile();

showPaths("test.txt");
showPaths("TEST.TXT");
showPaths("." + File.separator + "TEST.TXT");
showPaths(parent
+ File.separator + "."
+ File.separator + "test.txt");
showPaths(grandparent
+ File.separator + parentName
+ File.separator + ".."
+ File.separator + parentName
+ File.separator + "test.txt");
}
}
</pre></code>
On my system, I get the output:
<code><pre>
path: test.txt
absolute path: C:\Jim\Test\test.txt
canonical path: C:\Jim\Test\test.txt

path: TEST.TXT
absolute path: C:\Jim\Test\TEST.TXT
canonical path: C:\Jim\Test\test.txt

path: .\TEST.TXT
absolute path: C:\Jim\Test\.\TEST.TXT
canonical path: C:\Jim\Test\test.txt

path: C:\Jim\Test\.\test.txt
absolute path: C:\Jim\Test\.\test.txt
canonical path: C:\Jim\Test\test.txt

path: C:\Jim\Test\..\Test\test.txt
absolute path: C:\Jim\Test\..\Test\test.txt
canonical path: C:\Jim\Test\test.txt
</pre></code>
This shows how there can be many different paths (and absolute paths) to the same file, which all have the exact same canonical path. Thus canonical path is useful if you want to know if two different paths point to the same file or not.
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim! I was trying code too, but I think that the meaning of absolute confused me. Althought I don�t understand the meaning for the getAbsolute() method to exist, I mean: for what purpose I need to use this method?, but I �ll think about that.
Thanks again.
 
rajsim
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Canonical path is validated with the File System while
Absolute path is still abstract and may not be able to
represent any physical path.
Here is an example

Prints:
Absolute Path:Z:\a,b,b\a*b*c
Canonical Path:java.io.IOException: Invalid argument
at java.io.Win32FileSystem.canonicalize(Native Method)
at java.io.File.getCanonicalPath(File.java:437)
at Test.main(Test.java:9)
Note that the path contains invalid characters
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajsim: That�s a good example. Thank you very much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic