This is the snippet that saves the data entered by the user into the file. This works fine.i.e it creates the file at the wanted location but the problem is that the file created is empty. What is the reason for this ?
JFileChooser save = new JFileChooser();
int option = save.showSaveDialog( this );
if( option == JFileChooser.APPROVE_OPTION ) {
try {
BufferedWriter writer = new BufferedWriter( new FileWriter( save.getSelectedFile().getPath() + ".txt") );
String messageToBeSaved = jTextArea2.getText();
int lengthOfMessage = messageToBeSaved.length();
writer.write( messageToBeSaved, 0 , lengthOfMessage );
JOptionPane.showMessageDialog(new JFrame() , "Message saved");
} catch(Exception exc) {
System.out.println(exc);
}
If there is anything wrong with this snippet,please tell where i am going wrong.
Since it is a buffered writer, you should flush and close it.
Try adding this:
To be completely correct, you should close your writer in a
finallyblock.Edit: The flush is not really required since close will flush anyways.