【问题标题】:Java delimiter on a text file文本文件上的 Java 分隔符
【发布时间】:2021-03-16 16:44:47
【问题描述】:

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

      firstname             lastname             username             password 
           John                  Doe             $1234567                 $123
           Eden                 Hask             $1234576               $12345

最近一次登录(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;
        }
    };

【问题讨论】:

  • 为什么不直接使用制表符 ("\t") 来分隔字段?

标签: java regex delimiter


【解决方案1】:

我会完全不同:

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

【讨论】:

    【解决方案2】:

    根据您的描述,您似乎还想在一行结束时断开字符串。为此,请按以下方式添加回车符作为分隔大小写之一-

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

    这应该防止所有以前的“密码”字符串包含下一行的详细信息

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2019-12-07
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 2017-03-25
        • 2015-07-07
        • 1970-01-01
        • 1970-01-01
        • 2010-10-03
        相关资源
        最近更新 更多