1

I have a JFileChooser that is set as a set Save Dialog and a jTextArea where I can write stuff, my problem is that when I save it, the text doesn't get saved. Any ideas what I'm doing wrong?

private void saveAsButtonMouseClicked(java.awt.event.MouseEvent evt) {                                          
    SaveAs saveasbtn= new SaveAs();
    int retrival = saveOption.showSaveDialog(null);
    if (retrival == JFileChooser.APPROVE_OPTION) 
       {saveasbtn.saveAs(jTextArea2.getText(),saveOption);}
    }


 public void saveAs(String value,JFileChooser saveOption){

    try {
        FileWriter fw;
        fw = new FileWriter(saveOption.getSelectedFile()+".txt");
        fw.write(value);
    } catch (IOException ex) {
        Logger.getLogger(SaveAs.class.getName()).log(Level.SEVERE, null, ex);
    }
 }

3 Answers 3

1

You're not closing the FileWriter in saveAs method. Use the try-with-resources so the writer is closed and output is written to file. Maybe contrary to its name, FileWriter is also buffered, though it has smaller buffer than BufferedWriter, so contents are not written to disk immediately. The code would go like:

try (FileWriter fw = new FileWriter(saveOption.getSelectedFile()+".txt")) {
    fw.write(value);
} catch (IOException ex) {
    Logger.getLogger(SaveAs.class.getName()).log(Level.SEVERE, null, ex);
}

Alternatively, you can add a finally block after the catch and call fw.close() explicitly there (calling fw.flush() is redundant if closing the stream).

Sign up to request clarification or add additional context in comments.

Comments

1

Use the try-with-resources syntax to ensure your data is written out.

try (FileWriter fw = new FileWriter(saveOption.getSelectedFile()+".txt") {
    fw.write(value);
} catch (IOException ex) {
    Logger.getLogger(SaveAs.class.getName()).log(Level.SEVERE, null, ex);
}

You might also want to either let that IOException propagate from saveAs() (by adding throws IOException to the method signature) or do something else like return a boolean to indicate whether the write succeeded or not. Otherwise your program can't tell whether saveAs() actually worked or not.

Comments

1

You can force FileWriter to flush all written data to file immediately with fw.flush().

Do not forget to close the file at the end. After you do fw.close(), all data you've written will be automatically flushed to file and the file will be safely closed.

If you use try with resources, the file will be flushed and saved automatically.

 public void saveAs(String value,JFileChooser saveOption) {
    try(FileWriter fw = new FileWriter(saveOption.getSelectedFile()+".txt")) {
        fw.write(value);
    } catch (IOException ex) {
        Logger.getLogger(SaveAs.class.getName()).log(Level.SEVERE, null, ex);
    }
 }

1 Comment

Just calling fw.close() will suffice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.