How to add file extension to JFileChooser.showSaveDialog() ?
I'm building this basic, basic text editor. And the saving works fine, but I don't know how to automatically add a file extension to the output. Here's what the method looks like so far
Code :
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser c = new JFileChooser();
int x = c.showSaveDialog(null);
if (x == JFileChooser.APPROVE_OPTION) {
try {
String s = textAREA.getText();
BufferedWriter file = new BufferedWriter(new FileWriter(c.getSelectedFile()));
file.write(s);
file.close();
} catch (IOException ex) {
Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
}
}
how can I add a .txt to the file once u save it without typing it in myself?
--thanks for all the help
Re: How to add file extension to JFileChooser.showSaveDialog() ?
Simply modify the file object you get back:
Code Java:
File f = c.getSelectedFile();
String filePath = f.getPath();
if(!filePath.toLowerCase().endsWith(".txt"))
{
f = new File(filePath + ".txt");
}
// create your buffered writer from f
Re: How to add file extension to JFileChooser.showSaveDialog() ?
I guess that works. But other than appending the file path. Isn't there like a graphical option. When you save a word document it lets you chose the file type at the bottom "File Format: All Files". I know there's file filters in openDialog so I'm assuming there's something alike that for saving.
Re: How to add file extension to JFileChooser.showSaveDialog() ?
Yep, there is.
See: http://www.javaprogrammingforums.com...e-filters.html
However, I'm not sure if it will automatically add the extension on saves, if not you'll need to get the current file filter selected using getFileFilter() and attaching on the appropriate ending using the method above.
Re: How to add file extension to JFileChooser.showSaveDialog() ?
someone do it in a "rather" complex way, but works as you want...
check in this link java - How to save file using JFileChooser? - Stack Overflow