对如何使用BufferedReader读取User对象的arraylist感到困惑 [英] Confused on how to use BufferedReader for reading arraylist of User objects

查看:89
本文介绍了对如何使用BufferedReader读取User对象的arraylist感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注册用户后,他们可以登录,该登录应该使用BufferedReader从文本文件中读取其详细信息。但是,我不确定如何使用BufferedReader逐行读取User对象的arraylist来检查文本文件中的详细信息并允许用户登录。

After the user is registered, they can login which is supposed to read their details from a text file by using a BufferedReader. However, I am unsure of how to use a BufferedReader to read the arraylist of User objects line by line to check the details are in the text file and allow for the user to login.

这是我写的:

 public class LoginJFrame extends javax.swing.JFrame {
ArrayList<User> users = new ArrayList<>();

public LoginJFrame() {
    initComponents();
}



private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String nickname = edtNickname.getText();
    String loginID = edtLoginID.getText();
    String password = String.valueOf(edtPassword.getPassword());
    try {
        BufferedReader br = new BufferedReader(new FileReader("userinfo.txt"));
        String readFile = br.readLine();
        while (readFile != null) {
            String[] splitString = readFile.split(",");
            nickname = splitString[0];
            loginID = splitString[1];
            password = splitString[2];
            User user = new User(nickname, loginID, password);
            users.add(user);
            readFile = br.readLine();
            this.dispose();
            ThreadJFrame threadPage = new ThreadJFrame();
            threadPage.setVisible(true);

        }
    } catch (Exception e) {
        e.printStackTrace();
    }




}

我拥有另一个为字符串的arrayList的唯一原因是因为我无法找出BufferedReader逐行读取User arrayList的方式。

The only reason why I have another arrayList that is a string is because I cannot figure out a way for the BufferedReader to read my User arrayList line by line. Is there a way for the BufferedReader to read the arraylist class type instead of a string ArrayList?

User对象是一个单独的类,用于存储昵称,登录ID和密码,是否可以让BufferedReader读取arraylist类类型而不是字符串ArrayList?通过使用字符串。

The User object is a separate class that stores the nickname, login ID and password by using a string.

public class User  {
    String nickname;
    String loginID;
    String password;

public User(String nickname, String loginID, String password) {
    this.nickname = nickname;
    this.loginID = loginID;
    this.password = password;
}

public String getNickname() {
    return nickname;
}

public String getLoginID() {
    return loginID;
}

public String getPassword() {
    return password;
}

public void setNickname(String nickname) {
    this.nickname = nickname;
}

public void setLoginID(String loginID) {
    this.loginID = loginID;
}

public void setPassword(String password) {
    this.password = password;
}

@Override
public String toString() {
    return "User{" + "nickname= " + nickname + ", loginID= " + loginID + ", password= " + password + '}';
}


推荐答案

    currentLine = br.readLine();
    while (currentLine != null) {
        userstring.add(currentLine);
    }

那么,该代码仅读取一行,因为您仅调用了readline ()方法。

Well, that code only reads a single line since you only ever invoke the readline() method once.

代码应类似于:

    currentLine = br.readLine();

    while (currentLine != null) {
        userstring.add(currentLine);
        currentLine = br.readLine();
    }

并且由于您的 User数据包含在一行中(因为您只保存了User类的toString()),您可能需要使用 String.split(将数据解析为其单独的令牌。 ..)方法。

And since your data for the User is contained in a single line (since you just save the toString() of your User class) you will need to parse the data into its individual tokens probably by using the String.split(...) method.

当然,您的readToFile(...)方法实际上不会做任何事情,因为您在本地定义了ArrayList并不要从该方法返回任何数据,这样您的类的其余部分就无法访问该数据。

Of course your readToFile(...) method won't really do anything because you define the ArrayList locally and don't return any data from the method so the rest of your class can't access the data.

这篇关于对如何使用BufferedReader读取User对象的arraylist感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆