Hi, I am trying to enter a data from JtextField to notepad and need old data also in same notepad

.In each entering the old data is disappearing and only new data is showing there. I want both datas new and the old data. here is my code.:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
     String abc= jTextField1.getText().toString();
        PrintWriter writer = null;
        try {
            writer = new PrintWriter("textFieldOutput.txt", "UTF-8");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
writer.println(abc);
writer.close();
     }            

Answer

In each entering the old data is disappearing and only new data is showing there.

Use a FileWriter instead of the PrintWriter.

Then you can open the file in “append” mode.

Read the FileWriter API for the appropriate constructor to use.

You will also need to use the write(...) method instead of the println(..) method.

Typically you would also wrap the FileWriter in a BufferedWriter. Then you can use the newline() method as required.

Leave a Reply

Your email address will not be published. Required fields are marked *