首页 > 解决方案 > 如何逐行读取文件并存储特定值

问题描述

我需要一些帮助,因为我是 Java 新手,所以我已经坚持了一段时间。我在控制台中创建了一个登录面板,其中设置了默认用户名和密码来登录。他们可以创建一个帐户,将用户名存储在 username.txt 文件中,将密码存储在 passwords.txt 文件中。我想知道如何获取它,以便函数逐行读取用户名和密码的 txt 文件,直到它将用户名与用户名文件中的现有用户名匹配并且与密码相同,以便登录。

这是从文件读取功能:

public static void readFromFile() throws Exception {
    BufferedReader saveUsrFile, savePwFile;
    try {
        saveUsrFile = new BufferedReader(new FileReader("username.txt"));
        String line_u = saveUsrFile.readLine();

        while (line_u != null && saveUsrFile.readLine().equals(LP.username)) {
              cusUsr = saveUsrFile.readLine();
        }
        saveUsrFile.close();

        savePwFile = new BufferedReader(new FileReader("passwords.txt"));
        String line_p = savePwFile.readLine();

        while (line_p != null && savePwFile.readLine().equals(LP.password)) {
              cusPass = savePwFile.readLine();
        }
        savePwFile.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

这是登录面板:

import java.util.Scanner;

public class LoginPanel {

    static String username;
    static String password;

    public LoginPanel() {}

    public static void main(String[] args) {

        boolean flag = true;    
        Passenger p = new Passenger();
        Scanner input = new Scanner(System.in);

        while(flag == true) {
            System.out.println("Enter Username: ");
            username = input.nextLine();
            System.out.println("Enter Password: ");
            password = input.nextLine();

            if ((password.equals(p.DefPass) && username.equals(p.DefUser)) || (password.equals(p.cusPass) && username.equals(p.cusUsr))) {
                System.out.println("Passenger login successful!");
                p.display();
            } else {
                System.out.println("Please check your username or password and try again!");  
            }   
        }
    }   
}

有人请帮助我并告诉我如何让它逐行读取文件,直到它匹配并允许用户登录。

标签: javafileclassnetbeans

解决方案


一个简化的流程就像

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String username, password;
        System.out.print("Enter Username: ");
        username = input.nextLine();
        System.out.print("Enter Password: ");
        password = input.nextLine();

        if (authenticated(username, password)) {
            System.out.println("Passenger login successful!");
        } else {
            System.out.println("Please check your username or password and try again!");
        }
    }

    static boolean authenticated(String username, String password) {
        BufferedReader saveUsrFile, savePwFile;
        boolean usrFound = false, pwFound = false;
        try {
            saveUsrFile = new BufferedReader(new FileReader("username.txt"));
            String line_u = saveUsrFile.readLine();
            while (line_u != null) {
                if (line_u.equals(username)) {
                    usrFound = true;
                    break;
                }
                line_u = saveUsrFile.readLine();
            }
            saveUsrFile.close();

            savePwFile = new BufferedReader(new FileReader("password.txt"));
            String line_p = savePwFile.readLine();
            while (line_p != null) {
                if (line_p.equals(password)) {
                    pwFound = true;
                    break;
                }
                line_p = savePwFile.readLine();
            }
            savePwFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return usrFound && pwFound;
    }
}

示例运行:

Enter Username: hello
Enter Password: bye
Please check your username or password and try again!

另一个示例运行:

Enter Username: hello
Enter Password: Abc123
Passenger login successful!

username.txt 的内容:

hello
world
abc
xyz
hi
bye

password.txt 的内容:

Abc123
456Abc
Xyz123
456Xyz
123Abc
123Xyz

注意:在这个演示中,usernamepassword没有检查它们在各自文件中的顺序。


推荐阅读