嗨,我正在尝试从 JtextField 输入数据到记事本,并且在同一个记事本中也需要旧数据

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

。每次输入时,旧数据都会消失,只有新数据会显示在那里。我想要新数据和旧数据。这是我的代码。:

 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();
     }            

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

使用 FileWriter 而不是 PrintWriter

然后您可以在“追加”模式下打开文件。

阅读 FileWriter API 以使用适当的构造函数。

您还需要使用 write(...) 方法而不是 println(..) 方法。

通常您还会将 FileWriter 包装在 BufferedWriter 中。然后你可以根据需要使用newline()方法。