getCanonicalPath public String getCanonicalPath() throws IOException Returns the canonical pathname string of this abstract pathname. The precise definition of canonical form is system-dependent, but canonical forms are always absolute. Thus if this abstract pathname is relative it will be converted to absolute form as if by the getAbsoluteFile() method. Every pathname that denotes an existing file or directory has a unique canonical form. Every pathname that denotes a nonexistent file or directory also has a unique canonical form. The canonical form of the pathname of a nonexistent file or directory may be different from the canonical form of the same pathname after the file or directory is created. Similarly, the canonical form of the pathname of an existing file or directory may be different from the canonical form of the same pathname after the file or directory is deleted. Returns: The canonical pathname string denoting the same file or directory as this abstract pathname Throws: IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries Since: JDK1.1
"JavaRanch, where the deer and the Certified play" - David O'Meara
Assume there is a directory c:\temp\java File f = new File ("c:/temp/../temp/java/./.."); f.getCanonicalPath() = c:\temp\ f.getAbsolutePath() = c:\temp\..\temp\java\.\.. Canonical path is the bare minimum and neccesory path. AbsolutePath is the Complete path with with the File object was created. HTH, Paul.
Just to add... Consider your directory as in the explorer C:\Java\RancH and you are supplying argument to your program c:\java\ranch and here are the results. f.getAbsolutePath() writes c:\java\ranch and f.canonicalPath() writes C:\Java\RancH Hope that helps to understand the mear difference. Anna S Iyer.
Try: File f = new File("c:\\java\\ranch"); instead of : File f = new File("c:\java\ranch"); Though : File f= new File("c:"+ File.separator + "java"); is correct. It takes care of platform independence.