Java 文本文件的分隔符

Java delimiter on a text file

我正在尝试读取此格式的 txt 文件(不包括标题):

      firstname             lastname             username             password 
           John                  Doe             34567                 3
           Eden                 Hask             34576               345

最新的登录 (Eden) 总是有效,但之前的登录 (John) 无效。 出于某种原因,分隔符将包括所有详细信息,直到下一个 $。 我应该如何让我的定界符在密码后停止。

The user name is_1234567 and the password is_123
                Eden                 Hask!

The user name is_1234576 and the password is_12345!

这是完整的代码。

public void login() throws FileNotFoundException{  
        File file = new File("file.txt");
        Scanner read = new Scanner(file);
        found = false;

        read.useDelimiter("(\s*\$)");

        try {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Username: ");
            String username = keyboard.nextLine();
            System.out.print("Password: ");
            String passwordField = keyboard.nextLine();
            while(read.hasNext()){
                String user = read.next();
                String pass = read.next();
                System.out.println("The user name is_" + user + " and the password is_" + pass + "!\n");
                    if(user.trim().equals(username) && pass.trim().equals(passwordField)){
                        boolean found = true;
                        break;  
                    }        
                }
             if(found){
                return true;
             }
             else {
                return false;
             }
            read.close();
        }catch(Exception e){
            throw e;
        }
    };

根据您的描述,您似乎还想在一行结束时断开字符串。为此,按以下方式添加回车 return 作为定界案例之一 -

read.useDelimiter("\n|(\s*\$)")

这应该可以防止之前的所有 'password' 字符串包含下一行的详细信息

我会做完全不同的事情:

boolean found;
    public void login() throws FileNotFoundException {
        File file = new File("file.txt");
        Scanner read = new Scanner(file);

        try {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Username: ");
        String username = keyboard.nextLine();
        System.out.print("Password: ");
        String password = keyboard.nextLine();
        
        // Skip the first line
        read.nextLine();
        
        while (read.hasNextLine()) {
            String line = read.nextLine();
            //split current line with spaces into an array
            String[] lineArray = line.split("\s+");
            String user = lineArray[1];
            String pass = lineArray[4];
            System.out.println("The user name is " + user + " and the password is " + pass + "!\n");
            if (user.equals(username) && pass.equals(password)) {
                found = true;
                break;
            }
        }
        read.close();
        keyboard.close();
        
    } catch (Exception e) {
        e.printStackTrace();
    }
}

除了在分隔符中包含换行符之外,您可能还需要读取用户名以保持 userid/password 读取同步:

    read.useDelimiter("(\s*\$)|(\R)");

    try {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Username: ");
        String username = keyboard.nextLine();
        System.out.print("Password: ");
        String passwordField = keyboard.nextLine();
        while(read.hasNext()){
            String name = read.next();
            String user = read.next();
            String pass = read.next();