【发布时间】:2010-05-09 21:09:48
【问题描述】:
我的ArrayList 的单个条目是这样的:
public class Account {
String username;
String password;
}
我设法将一些“帐户”放在一个文本文件中,但现在我不知道如何阅读它们。
这就是我的ArrayList 在文本文件中的样子:
用户名1 密码1 |用户名2 密码2 |等
这是我想出的代码的一部分,但它不起作用。
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");
}
【问题讨论】:
-
两天前问的问题几乎完全相同:stackoverflow.com/questions/2788080/reading-a-text-file-in-java您的情况有点复杂,但这些答案对您仍然有用。
-
以后请多注意代码格式。将其重新格式化为可读文件所花费的时间超出了应有的时间。
-
嗯,这似乎是某种续集:stackoverflow.com/questions/2777107/…
-
事情并没有完全按照应有的方式进行,所以我不得不再次询问。很抱歉给您带来不便
-
@RichH:我没有做过这样的事情(无论如何是故意的)。我只是在编辑标签;页面结束了;不得不以某种方式修复它。请注意,SO 历史记录不会显示所有更改,并且编辑冲突比其他一些系统更成问题。