aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Canonical vs Absolute" Watch "Canonical vs Absolute" New topic
Author

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
Wanderer
Sheriff

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

"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.
rajsim
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

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

Joined: Jun 28, 2000
Posts: 477
Rajsim: That�s a good example. Thank you very much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Canonical vs Absolute
 
Similar Threads
Help need: Book K&S Chapter 7, Exercise Q=9
Window contained in JFrame
Casting doubt
complex one-to-many
Change Of Faith