问题
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.
This is what I have written:
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();
}
}
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?
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 + '}';
}
回答1:
currentLine = br.readLine();
while (currentLine != null) {
userstring.add(currentLine);
}
Well, that code only reads a single line since you only ever invoke the readline() method once.
The code should be something like:
currentLine = br.readLine();
while (currentLine != null) {
userstring.add(currentLine);
currentLine = br.readLine();
}
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.
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.
来源:https://stackoverflow.com/questions/49459403/confused-on-how-to-use-bufferedreader-for-reading-arraylist-of-user-objects