Re: java getPath getAbsolutePath getCanonicalPath and JFileChooserreuse

Discussion in 'Java' started by Jon A. Cruz, Jul 9, 2003.

  1. Jon A. Cruz

    Jon A. Cruz Guest

    Krick wrote:
    > Can someone explain why I should use of the following java File
    > methods over any of the others...
    > getPath()
    > getAbsolutePath()
    > getCanonicalPath()
    > They seem to always return the same thing in my testing.

    Then you need to test some different cases.


    For one, try paths with ".." in them. That is, the common "back up one
    directory" expression.

    For another, try paths using hard and soft symlinks. (Hmmm... might need
    Unix or Linux for that one). Or aliases. Etc.

    Try some paths that start at the root of your filesystem.

    Try some paths that start with ".."

    But most importantly...

    Try reading the API docs on them. They actually cover a lot of that.

    > Ultimately, what I am trying to do is give the user a JFileChooser to
    > open a file. After they've selected a file and hit ok, I want to
    > cache the path to the directory that the chosen file was in so I can
    > use it as a starting directory for subsequent creations of
    > JFileChooser.


    Read the API docs and it will be quite clear to you.
    Or... look up "canonical" in the dictionary for a clue. They named it
    that for a reason. :)

    > Hmm... I just had a thought... Can I create and re-use a single
    > instance of JFileChooser? If so, does it retain the path where it
    > was when it was last closed?

    Instead, how about just asking the JFileChooser?

    // Once a selection is made (and not canceled)
    mlastDir = myChooser.getCurrentDirectory().getCanonicalFile();

    Jon A. Cruz, Jul 9, 2003
    1. Advertisements

  2. Roedy Green

    Roedy Green Guest

    Re: java getPath getAbsolutePath getCanonicalPath and JFileChooser reuse

    On Wed, 09 Jul 2003 08:51:27 -0700, "Jon A. Cruz" <>
    wrote or quoted :

    >Then you need to test some different cases.

    I think Star Trek scared people off experimenting. If anyone made an
    error, the computer invariably exploded. In real life, you can learn
    a lot from just trying out methods with a variety of data and see what
    happens. Gradually the light dawns and the documentation begins to
    make sense. Looking at Sun's source code in also helps too.

    With Slickedit, I just right click on any method, variable or class
    and I can see the corresponding source code in along with

    Sun does not like to use examples. I do. Quite often you will find
    examples of what the various methods do in the Java glossary,
    including the various file naming methods.


    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See for The Java Glossary.
    Roedy Green, Jul 9, 2003
    1. Advertisements

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. Roedy Green
    Roedy Green
    Jul 9, 2003
  2. RonRan

    Doubt in getCanonicalPath

    RonRan, Nov 21, 2005, in forum: Java
    Nov 24, 2005
  3. Chris Uppal
    Chris Uppal
    Jun 27, 2006
  4. Sathyaish
  5. Connolly

    GetPath to variable

    Connolly, Dec 11, 2007, in forum: Python

Share This Page