如何使用FileReader逐行读取

时间:2015-11-24 11:29:01

标签: java swing filereader readfile

感谢您的关注。

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

在我的代码中,我使用了Register文件的写文件和Login Form的Read文件。但是,它不起作用。我在用于读取文件的代码中知道我的问题。你可以帮我实现吗?。

非常感谢你的帮助。

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

3 个答案:

答案 0 :(得分:5)

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

 String line;

    try {

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


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

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

答案 1 :(得分:2)

假设我们的文件usernamehash存储如下:

  

Hello World
  测试测试
  DumbUser 12345
  用户sjklfashm-0998()&

我们希望将每行中的第一个单词用作username,将第二个单词用作password / hash。然后想法是:

  • 阅读一行
  • 将行拆分为“”
  • 的部分
  • 将第一部分与username进行比较,将第二部分与pass
  • 进行比较
  • 如果匹配返回true,则重新开始

这导致此代码:

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

标记了我的代码与您的代码不同的部分

答案 2 :(得分:0)

您可以使用以下代码阅读文件..

    try {
        BufferedReader bufferreader = new BufferedReader(new FileReader("./users/Ender/Desktop/GetUser/submit.txt"));
        String line;

        while ((line = bufferreader.readLine()) != null) {
            // line variable contains the readed line
            // You can append it to another String to get the whole text or anything you like
        }

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

如果您想使用以下代码编写文件..

    BufferedWriter writer = new BufferedWriter(new FileWriter("./users/Ender/Desktop/GetUser/submit.txt")); 
    writer.write(your_text);
    writer.close();

如果要附加文本,请使用以下代码创建BufferedWriter

的实例
    BufferedWriter writer = new BufferedWriter(new FileWriter("/users/Ender/Desktop/GetUser/submit.txt", true));