我正在寻找一种简单的方法来打开.txt
文件,修改它并实时读取所应用的更改。 我创建了一个登录面板,其中有两个用户插入ID/pass的JTextField
。在Java中打开,修改和读取.txt文件更改的最简单方法是什么?
我想实现一个更改密码方法,所以我需要打开.txt
文件,删除旧密码并写入新密码(或覆盖)。然后我必须再次读取文件以更新密码。
这是该小组的代码:
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()) && 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();
}
}
}
这些都是file.txt的内容: 用户 密码
如果您要修改文件,则不需要重新读取它。您已经知道这些修改。也没有修改文件这样的事情。你可以附加到它,或者你可以完全覆盖它。 – Kayaman
@Kayaman可以覆盖文件的某些部分,而无需完全覆盖它。它不可能删除或插入数据,只能改变它。 – puhlen
进入file.txt我有2个字:一个用户的ID,一个用户的密码。 我需要用新密码删除/覆盖第二行。 –