내 ArrayList
의 단일 항목의 형식은 :Java의 텍스트 파일에서 ArrayList를 읽는 방법?
public class Account {
String username;
String password;
}
나는 텍스트 파일에 약간의 "계정"을 넣어 관리,하지만 지금은 내가 그들을 읽는 방법을 모른다. 이 내 ArrayList
텍스트 파일에 모습입니다 :
username1 password1 | username2 password2 | etc
이 내가 생각 해낸 코드의 일부이지만, 그것은 작동하지 않습니다.
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");
}
2 일 전 거의 똑같은 질문을했습니다. http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java 사례가 좀 더 복잡하지만 답변은 여전히 당신에게 유용 할 것입니다. – Jonik
앞으로는 코드 서식을 신중하게 선택하십시오. 읽을 수있는 형식으로 다시 포맷해야하는 것보다 더 많은 시간이 걸렸습니다. –
흠, 이런 종류의 속편처럼 보입니다. http://stackoverflow.com/questions/2777107/how-to-read-data-from-file-into-array-java – Jonik