This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Canonical vs Absolute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Canonical vs Absolute" Watch "Canonical vs Absolute" New topic

Canonical vs Absolute

Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
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

Joined: Jan 30, 2000
Posts: 18671
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:

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());

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();

showPaths("." + File.separator + "TEST.TXT");
+ File.separator + "."
+ File.separator + "test.txt");
+ File.separator + parentName
+ File.separator + ".."
+ File.separator + parentName
+ File.separator + "test.txt");
On my system, I get the output:
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
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.

"I'm not back." - Bill Harding, Twister
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
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.
Ranch Hand

Joined: May 31, 2000
Posts: 116
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

Absolute Path:Z:\a,b,b\a*b*c
Canonical Invalid argument
at Method)
at Test.main(
Note that the path contains invalid characters
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
Rajsim: That�s a good example. Thank you very much!
Consider Paul's rocket mass heater.
subject: Canonical vs Absolute
It's not a secret anymore!