[英]What is the simplest way to open, modify and read the changes on a .txt file in Java?
我正在尋找一種打開.txt
文件,對其進行修改並實時讀取對其應用的更改的簡單方法。 我創建了一個登錄面板,其中包含兩個JTextField
,用戶可在其中插入ID /密碼。
我想實現一個更改密碼的方法 ,因此我需要打開.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的內容:用戶密碼
當我們閱讀文件時,通常使用String來存儲文件的內容,但是問題是字符串是不可變的,因為它們不能被編輯,但是您想要做的事情可以通過這種方式完成:
然后讀取該內容並將其存儲在String變量中。
使用替換方法進行更改。
然后再次將修改后的String重寫到您的文件中。
一種方法是從文件中讀取文本行,並且由於您使用的是空間定界,因此可以使用string.split(" ")
將字符串拆分為一個數組。 更新string[1]
的密碼,將字符串重新設置為username password
,然后使用`FileOutputString(...,false)將其寫入文件以覆蓋內容。
如果計划根據需要將文件保持打開狀態以進行定期更新,則可以利用RandomAccessFile
維護指向最后一次seek
的指針,並使用' '
覆蓋密碼,以掩蓋先前密碼遺留下來的先前字符。
無論哪種方式,這都是我的建議,但是,正如其他人已經指出的那樣,出於安全原因和易於維護的目的,使用數據庫要容易得多。 在我工作的地方,我只能訪問這樣的數據庫,並且最近需要對表中的密碼進行加密。 使用加密技術也可能是您的願望。
在您的情況下:
Path path = Paths.get("... .txt");
List<String> = Files.readAllLines(path, StandardCharsets.UTF_8);
user = lines.get(0);
password = lines.get(1);
code = lines.get(2);
List<String> lines = new LinkedList<>();
Collections.addAll(lines, user, password, code);
Files.write(path, lines, StandardCharsets.UTF_8);
您還可以將.properties與key = value對一起使用,並使用XML格式的變體。
如果您考慮以后使用新格式更改軟件,那么數據中的版本號也可能會很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.