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.