Doubt in getCanonicalPath

R

RonRan

Hi,

The javadoc for the getCanonicalPath() method of java.io.File class
mentions the following :

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

Can someone please explain this with some concrete example? I am
confused as to how a nonexistent file can have a canonical path and
also how it may change when that file is created.

Thanks,
Rohan
 
A

adamspe

A non-existent file has to be able to have a canonical path.
java.io.File is only an abstract representation of a file name (or
directory name) regardless of whether the file exists or not. It's
nothing but a holder for the reprepresentation of the path to a file
system object.

I'm guessing the bit of verbage surrounding canonical forms of existing
and non-existent paths has to do with OS-dependant features, probably
specifically case sensitivity on Win32.

On UNIX
/home/joeuser/MyFile
and
/usr/joeuser/myFile

are distinct and can co-exist without collisions

The same isn't true of a similar situation on Win32.

For example:

Say on Windows you have a non-existent file (or more appropriately
don't have)

String canonical = (new File ( "/tmp/NonExistent.txt"
)).getCanonicalPath();

On Windows, if the file didn't exist, would return something like
"C:\tmp\NonExistent.txt"

Say you then created that file but with case like "nonExistent.txt"

Runing the -exact- same bit of code above (no change in case on the
constructor) would result in a canonical representation of:

"C:\tmp\nonExistent.txt"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top