title: Nothing gets saved in the file
body: <p>This is the snippet that saves the data entered by the user into the file. This works fine.<strong>i.e it creates the file at the wanted location</strong> but the problem is that the file created is empty. What is the reason for this ?</p>
  
  <pre><code>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);
              }
  </code></pre>
  
  <p>If there is anything wrong with this snippet,please tell where i am going wrong.</p>