how to read an arraylist from a txt file in java?
Posted
by lox
on Stack Overflow
See other posts from Stack Overflow
or by lox
Published on 2010-05-09T21:09:48Z
Indexed on
2010/05/09
21:18 UTC
Read the original article
Hit count: 310
how to read an arraylist from a txt file in java?
my arraylist is the form of:
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 look 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 logic 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");
}
}
© Stack Overflow or respective owner