How to read an arraylist from a txt file in java?
My arraylist is of the form:
public class Account {
String username;
String password;
}
I managed to put some "Accounts" in the a txt file, but now I don't know how to read them. This is how my arraylist looks in the txt file:
username1 password1 | username2 password2 | etc
This is a part of the code I came up with, but it doesn't work. It looks logical to me though... :) .
public static void RdAc(String args[]) {
ArrayList<Account> peoplelist = new ArrayList<Account>(50);
int i,i2,i3;
String[] theword = null;
try {
FileReader fr = new FileReader("myfile.txt");
BufferedReader br = new BufferedReader(fr);
String line = "";
while ((line = br.readLine()) != null) {
String[] theline = line.split(" | ");
for (i = 0; i < theline.length; i++) {
theword = theline[i].split(" ");
}
for(i3=0;i3<theline.length;i3++) {
Account people = new Account();
for (i2 = 0; i2 < theword.length; i2++) {
people.username = theword[i2];
people.password = theword[i2+1];
peoplelist.add(people);
}
}
}
}
catch (IOException ex) {
System.out.println("Could not read from file");
}