getPath() problem

i'm creating a file editor. i'm at a stage where i'm designing the 'save as' option.

whenever save as is clicked a FileChooser pops up with 'savedialog' and would allow the user to choose the directory and a name for the file.

after entering the file name when save is clicked, following gets executed..

int returnValue = filechooser.showSaveDialog(fc_frame);

if(returnValue == filechooser.APPROVE_OPTION)

{

File f = filechooser.getSelectedFile();

String temp = f.getPath();

temp = temp +"\\" + filechooser.getName() + ".txt";

w.write(temp);

this will get the path. example "c:\java", add "\" to it. then add the file name i have typed, example 'a' and then finally add '.txt' to it.

so the effect would be to creat a string like...C:\java\a.txt. but instead it adds "\null after each file name. so what happnes is...

"c:\java\a\null.txt

as a result, createNewFile() throws io exception.

to sum it up. i want to enter a the file name in the file chooser and when i hit save it should create a new file in the current direc tory.

can someone help me with this?

Neerav

[1365 byte] By [kothari_neerava] at [2007-11-26 22:54:16]
# 1
EDIT: <deleted, bad response>Message was edited by: aberrant80
aberrant80a at 2007-7-10 12:18:05 > top of Java-index,Java Essentials,Java Programming...
# 2
Well I don't know, but a log/debug statement (or the docs) could reveal. It looks to me like f.getPath() returns "c:\java\a" and filechooser.getName() returns null. If this is a stable behaviour, I guess al you have to do is append ".txt" to temp?
OleVVa at 2007-7-10 12:18:05 > top of Java-index,Java Essentials,Java Programming...
# 3
I'm thinking that by calling getSelectedFile(), the file f would already have the name that was typed into the file chooser (according to the javadoc).
aberrant80a at 2007-7-10 12:18:05 > top of Java-index,Java Essentials,Java Programming...
# 4

String getName(File f)

Returns the filename.

File getSelectedFile()

Returns the selected file.

that was from the api.

getSelectedFile() returns a File object handle.

so i wrote...

FIle f = new filechooser.getSelectedFile();

getPath() returns a string with the path of the file. example, if the file is c:\java\a.txt then the path is c:\java

so i wrote..

String temp = f.getPath();

and

getName() returns the name of the file that i typed in the text filed of file chooser.

actually, the correct subject of the topic would have been getName() problem rather than getPath() problem.

kothari_neerava at 2007-7-10 12:18:05 > top of Java-index,Java Essentials,Java Programming...
# 5
u were right olew. there was no need for getname. getpath was enough. thanks a lot.Neerav
kothari_neerava at 2007-7-10 12:18:05 > top of Java-index,Java Essentials,Java Programming...
# 6
Could it be that your FileChooser is in DIRECTORIES_ONLY selection mode instead of FILES_ONLY selection mode? It could explain the results: getSelectedFile is a directory, getName is null because there's no file selected, getPath returns everything you selected...
Peetzorea at 2007-7-10 12:18:06 > top of Java-index,Java Essentials,Java Programming...