【问题标题】:How to read a file line by line and store a specific value如何逐行读取文件并存储特定值
【发布时间】:2020-02-26 03:35:17
【问题描述】:

我需要一些帮助,因为我是 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!");  
            }   
        }
    }   
}

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

【问题讨论】:

  • 您检查之前的读数是否为非 null,然后在 while 循环的头部再次读取。似乎不是你想要的。在循环之前读取,然后检查循环头中的非 null 并在循环中再次读取,因为最后一条语句可能是您要查找的内容

标签: java file class netbeans


【解决方案1】:

简化的流程是这样的

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 不会检查它们在各自文件中的顺序。

【讨论】:

  • 非常感谢!你是救生员:)
【解决方案2】:

我建议使用 Java NIO。你有一个很好的方法,它从文件中返回行流,这很酷,因为你可以像这样做所有这些花哨的流的东西:

String password = "givenPassword";
Path path = Paths.get("passwordFileName");
Optional<String> passwordOptional = Files.lines(path)
      .filter(line -> password.equals(line))
      .findAny();
boolean isPasswordFound = passwordOptional.isPresent();

【讨论】:

    猜你喜欢
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    相关资源
    最近更新 更多