canonical or absolute path?

R

Richard Chrenko

Although the Java SDK documentation defines "absolute" and "canonical"
paths, the practical meaning is not clear to me. For example, if I want my
application to save a list of "recent files", which should I use?
 
S

Steve W. Jackson

Richard Chrenko said:
:Although the Java SDK documentation defines "absolute" and "canonical"
:paths, the practical meaning is not clear to me. For example, if I want my
:application to save a list of "recent files", which should I use?

I did some tinkering a few weeks ago with the two of them. While I'm
not 100% certain this is the distinction, I believe that what I found
was this. If a File object's path at creation has some kind of relative
reference used (such as ../otherdir/filename), then the canonical path
appeared to me to replace the ".." portion with the appropriate text.
The absolute path appeared to contain the path up to and including the
current working directory (or other source directory, depending on how
you created it, and then *included* the ".." portion, thus making it
longer.

If I'm correct in what I believe I understood, I would use canonical
paths wherever possible.

= Steve =
 
J

Jon A. Cruz

Steve said:
I did some tinkering a few weeks ago with the two of them.

Yup. That's pretty much it.

Absolute allows you to access the file from any current position, since
it's a path from the top of the file system. Canonical is 'the' way to
write a path, and should always be the same.

http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=canonical
If I'm correct in what I believe I understood, I would use canonical
paths wherever possible.

Probably... but there is the gotcha that it can throw exceptions.

BTW, the javadoc for these methods have all those details.

http://java.sun.com/j2se/1.4.1/docs/api/java/io/File.html#getAbsolutePath()

http://java.sun.com/j2se/1.4.1/docs/api/java/io/File.html#getCanonicalPath()

There are a few more things specified (like when a canonical path can
change), so you should read the docs.
 
A

Anton Spaans

Conceptually, there is a rather big difference between canonical path and
absolute path.

This is what is usually understood by the word 'canonical':
- Values of Objects have representations, usually String representations
(from now on, I only will refer to them as String representations, since
they deal with user-input and user readable output). Each representation
must be *valid* for that particular object. However, one Object can have
multiple representations. For example, take an Integer that contains the
value 5. Valid representations are "5", "00005", "+5", "5.0", "0.5e+01",
etc. (invalid representations are "Hello", "QQlq"..hehehe). But there is
exactly 1 *canonical* representation of this Integer! The canonical
representation is one from the set of valid representations. Which one...?
That is usually agreed upon by commitees, drafts (RFCs), mathematical
notations, etc. In this case, the Integer's canonical representation would
be "5".

File-paths have something similar:
1- A file can have many relative paths.
2- Canonical paths are absolute paths.
3- An absolute path is not necessarly a canonical path! This holds true
especially under Unix, which support symbolic links. Under Windows, an
absolute path is usually a canonical path.

Example:
- A file with canonical path "/usr/somedir/subdir/file.txt"
- The 'current directory' is "/usr/somedir/subdir2"
- Subdir2 contains a symbolic link to this "file.txt" file, called "symlink"

Then these are valid relative paths to file.txt
"../subdir/file.txt", "../../somedir/subdir/file.txt",
"././././././file.txt"

These are valid absolute paths to file.txt:
"/usr/somedir/subdir/file.txt", "/usr/somedir/subdir2/symlink"

But only one out of all the valid paths is the canonical. It probably is
this one:
"/usr/somedir/subdir/file.txt"

How a canonical path is exactly defined for a File, you may have to check
from RFCs for that.

-- Anton Spaans.
 

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,774
Messages
2,569,596
Members
45,129
Latest member
FastBurnketo
Top