canonical or absolute path?

Discussion in 'Java' started by Richard Chrenko, Jan 23, 2004.

  1. 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?
     
    Richard Chrenko, Jan 23, 2004
    #1
    1. Advertising

  2. In article <>,
    Richard Chrenko <> wrote:

    >: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 =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Jan 23, 2004
    #2
    1. Advertising

  3. Richard Chrenko

    Jon A. Cruz Guest

    Steve W. Jackson wrote:
    > In article <>,
    > Richard Chrenko <> wrote:
    >
    >
    >>: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.


    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.
     
    Jon A. Cruz, Jan 23, 2004
    #3
  4. Richard Chrenko

    Anton Spaans Guest

    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.




    "Richard Chrenko" <> wrote in message
    news:eek:...
    > 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?
     
    Anton Spaans, Jan 23, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    19,617
    Sudsy
    Jul 8, 2004
  2. Replies:
    10
    Views:
    3,648
  3. Thomas Guettler
    Replies:
    3
    Views:
    771
    Andrei
    Oct 27, 2003
  4. Nigel Wilkinson
    Replies:
    2
    Views:
    398
    Nigel Wilkinson
    Jul 25, 2005
  5. James Byrne
    Replies:
    3
    Views:
    577
    James Byrne
    Sep 14, 2010
Loading...

Share This Page