如何使用 FileReader 逐行读取

新手上路,请多包涵

感谢您的关注。

我创建了一个程序,我正在使用登录表单和注册表单。一旦用户注册了他们的电子邮件,他们的密码将被保存到 submit.txt 中。然后他们将返回登录表单并输入保存到 submit.txt 中的电子邮件和密码。

在我的代码中,我对注册表单使用写入文件,对登录表单使用读取文件。但是,它不起作用。我在用于读取文件的代码中知道我的问题。你能帮我意识到这一点吗?

非常感谢您的帮助。

  if (checkPassword(usern, hash)) {
    System.out.println("Logged in!");
    ChooseWindow ptb = new ChooseWindow();
    ptb.setVisible(true);
    LoginWindow.this.dispose();
 } else {
    System.out.println("Wrong password");
 }

 public boolean checkPassword(String username, String pass) {
    try {
        FileReader inFile = new  FileReader("/users/Ender/Desktop/GetUser/submit.txt");
        BufferedReader inStream = new BufferedReader(inFile);
        String inString;

        while ((inString = inStream.readLine()) != null) {}
        inStream.close();
    }catch (IOException e) {
        e.printStackTrace();
    }
    return false;
 }

原文由 Ender phan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 414
2 个回答

这是我从文件中读取的代码:

  String line;

    try {

        BufferedReader bufferreader = new BufferedReader(new FileReader("C:\\Users\\ahmad\\Desktop\\aaa.TXT"));

        while ((line = bufferreader.readLine()) != null) {
          /**
            Your implementation
          **/

        }

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

原文由 Ahmad Alkhatib 发布,翻译遵循 CC BY-SA 4.0 许可协议

假设我们有一个文件 usernamehash 存储如下:

你好世界

测试一下

愚蠢的用户 12345

用户 sjklfashm-0998() ( &

我们想将每行中的第一个单词用作 username ,将第二个单词用作 password / hash 。那么思路是:

  • 读一行
  • 在“”处将线分成几部分
  • 将第一部分与 username 比较,第二部分与 pass 比较
  • 如果匹配返回真,否则重新开始

导致此代码:

 public boolean checkPassword(String username, String pass) {
    // if there is no username or no password you can not log in
    if(username == null || pass == null){ // diff
        return false;                     // diff
    }                                     // diff
    try {
        FileReader inFile = new  FileReader(PASSWORD_FILE);
        BufferedReader inStream = new BufferedReader(inFile);
        String inString;

        while ((inString = inStream.readLine()) != null) {
            // we split every line into two parts separated by a space " "
            String usernameHash[] = inString.split(" "); // diff
            // if there are more or less than two parts this line is corrupted and useless
            if(usernameHash.length == 2                  // diff
                    && username.equals(usernameHash[0])  // diff
                    && pass.equals(usernameHash[1])){    // diff
                // if username matches the first part and hash the second everything is goo
                return true;                             // diff
            }                                            // diff
        }
        inStream.close();
    }catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

我用 // diff

原文由 Aracurunir 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题