Extension problem using JFileChooser
Can anyone please help me with this? I'm using a JFileChooser to create new files, I want it to save them in a ".txt" format, the problem is the user has to explicitely add ".txt" to his file name, otherwise it will save it as "what_the_user_has_typed" without any extension...
and after I recover the filename the user has typed, there's no function to change the name as "setName"
And the renameTo(new File()); fails, can I program it so it automatically adds the ".txt" extension to what the user has typed?
File file = new File();
Thank you :)
Re: Extension problem using JFileChooser
after I recover the filename the user has typed, there's no function to change the name as "setName"
where file is as returned by the dialog, and newName is the name you would prefer to use.
file = new File(file.getParent(), newName);
Strictly speaking ".txt" is an extension not a format; files are like roses in this respect.
This isn't just a quibble. It's up to your program what format you save a file in, but you should be a bit careful about rejecting the name that the user wishes to give it. I don't appreciate the likes of Notepad being "helpful". Perhaps add a checkbox "add .txt" or something, and respect the filename if it has a . in it.