以下数据位于 Admin.txt 文件内
id、用户名、密码、姓名、电子邮件、联系人号码、icNumber
AD001|admin|admin|admin|admin@gmail.com|0123456789|931245678976
AD002|jeff|jeff|jefferyleo|jefferyleo@gmail.com|12345678790|941457431246
我应该如何将这些数据读入数组列表?
List<Admin> adminList = new ArrayList<Admin>();
这是我贴花的 arrayList。
BufferedReader br = new BufferedReader(new FileReader("Admin.txt"));
String strLine;
while((strLine = br.readLine()) != null)
{
String[] values = strLine.split("|");
adminList.add(new Admin());
}
最佳答案
也许这段代码可以帮助你
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("Admin.txt"));
ArrayList<Admin> al = new ArrayList<Admin>();
String line = null;
while ((line = br.readLine()) != null) {
Admin admin = new Admin();
String lines[] = line.split("|");
/*adjust accordingly
admin.setName(lines[0]);
admin.setUser(lines[1]);
....*/
al.add(admin);
}
br.close();
}
关于java - 如何将txt文件数据读入arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21288895/