Java에서 .txt 파일의 변경 사항을 열고 수정하고 읽는 가장 간단한 방법은 무엇입니까?

Mattia_B

.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의 내용입니다. 사용자 비밀번호

Joop Eggen

귀하의 맥락에서 :

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

XML 형식의 변형과 함께 키 = 값 쌍과 함께 .properties를 사용할 수도 있습니다.

나중에 새로운 형식으로 소프트웨어를 변경할 생각이라면 데이터의 버전 번호도 좋을 것입니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관