aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String getCanonicalPath() ???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String getCanonicalPath() ????" Watch "String getCanonicalPath() ????" New topic

String getCanonicalPath() ????

Amit Roy
Ranch Hand

Joined: Oct 10, 2000
Posts: 132
String getCanonicalPath()-Returns the canonical form of this File object's pathname
the above is a method in the File class can some one pls explain me as to what this method exactly does

<I>Chance Favours the Prepared minds"</I>
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Per the Sun API:

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.
The canonical pathname string denoting the same file or directory as this abstract pathname
IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries

"JavaRanch, where the deer and the Certified play" - David O'Meara
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3314
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.

Get Certified, Guaranteed!
(Now Revised for the new Pattern)

Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Anna s Iyer

Joined: Aug 21, 2000
Posts: 24
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.
faiza haris
Ranch Hand

Joined: Oct 17, 2000
Posts: 173
Paul....i get errors if i just write
File f = new File("c:\java\ranch");
and its correct as below
File f= new File("c:"+ File.separator + "java");
any details? im missing?
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
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.

It is sorta covered in the JavaRanch Style Guide.
subject: String getCanonicalPath() ????