title: What is the simplest way to open, modify and read the changes on a .txt file in Java?
body: <p>I'm looking for a simple way to open a <code>.txt</code> file, <strong>modify it</strong> and read in real-time the changes applied on it.
  I created a login panel, with two <code>JTextField</code> where user inserts ID/pass.</p>
  
  <p>I want to implement a <strong>change password method</strong>, so I need to open the <code>.txt</code> file, delete the old password and write the new one (or overwrite). Then I have to read again the file to update the password.</p>
  
  <p>That's the panel's code:</p>
  
  <pre><code>import static java.awt.Color.WHITE;
  import java.awt.Font;
  import java.awt.Graphics;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.awt.image.BufferedImage;
  import java.io.BufferedReader;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  import linteati_ced.GUI.frame.MainFrame;
  import linteati_ced.HotArea;
  import static linteati_ced.utils.Constants.*;
  import linteati_ced.utils.Resources;
  
  public class LoginPanel extends JPanel {
  
      private MainFrame mf;
  
      private BufferedImage background;
  
      private FileReader file;
      private BufferedReader reader;
  
      private JTextField userArea, passwordArea;
      private String user, password;
  
      private HotArea confirm;
      private HotArea exit;
      private String message;
  
      private Thread dialogue;
      private String code;
  
      public LoginPanel(MainFrame mainFrame) throws FileNotFoundException, IOException {
          this.setSize(PANEL_X, PANEL_Y);
          this.setLayout(null);
  
          this.mf = mainFrame;
  
          this.background = Resources.getImage(BACKGROUND_LOGIN);
          this.message = DEFAULT_MESSAGE;
  
          this.confirm = new HotArea(1178, 922, 60, 60);
          this.exit = new HotArea(1178, 25, 60, 60);
  
          this.file = new FileReader(Resources.extract(LOGIN));
          this.reader = new BufferedReader(file);
  
          this.userArea = new JTextField("");
          this.userArea.setBounds(600, 460, 200, 30);
          this.userArea.setFont(new Font("Arial", 0, 24));
          this.passwordArea = new JTextField("");
          this.passwordArea.setBounds(600, 550, 200, 30);
          this.passwordArea.setFont(new Font("Arial", 0, 24));
  
          this.add(this.userArea);
          this.add(this.passwordArea);
  
          try {
              this.user = reader.readLine();
              this.password = reader.readLine();
              this.code = reader.readLine();
              System.err.println(this.user);
              System.err.println(this.password);
          } catch (IOException ex) {
          }
  
          file.close();
  
          this.addMouseListener(new MouseAdapter() {
  
              @Override
              public void mousePressed(MouseEvent e) {
  
                  if (confirm.isClicked(e)) {
                      if (user.equals(userArea.getText()) &amp;&amp; password.equals(passwordArea.getText())) {
                          mf.log_in();
                      } else {
                          System.out.println("Error");
                          message = ERROR_MESSAGE;
                          dialogue = new Thread(new MessageDialogue());
                          dialogue.start();
                          repaint();
                      }
                  }
  
                  if (exit.isClicked(e)) {
                      System.exit(0);
                  }
              }
          });
      }
  
      public String getCode() {
          return this.code;
      }
  
      public void resetTextField() {
          this.userArea.setText("");
          this.passwordArea.setText("");
      }
  
      @Override
      protected void paintComponent(Graphics g) {
          g.drawImage(background, 0, 0, null);
          g.setColor(WHITE);
          g.setFont(new Font("Arial", 0, 22));
          g.drawString(message, 560, 660);
      }
  
      private class MessageDialogue implements Runnable {
  
          @Override
          public void run() {
              try {
                  Thread.sleep(1500);
              } catch (InterruptedException ex) {
              }
              message = DEFAULT_MESSAGE;
              repaint();
          }
  
      }
  
  }
  </code></pre>
  
  <p>These are the contents of the file.txt:
  user
  password</p>