Re: java getPath getAbsolutePath getCanonicalPath and JFileChooserreuse

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();


....
 
R

Roedy Green

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 src.zip also helps too.

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

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.

see http://mindprod.com/jgloss/file.html
 

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,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top