J
Jon A. Cruz
Krick said: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.
*****
http://java.sun.com/j2se/1.4.1/docs/api/java/io/File.html
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.
Ahhh....
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?
http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFileChooser.html#getCurrentDirectory()
// Once a selection is made (and not canceled)
mlastDir = myChooser.getCurrentDirectory().getCanonicalFile();
....