Java 将文本文件数据添加到Arraylist
我想将文本文件数据添加到ArrayList。 这是我的文本文件Java 将文本文件数据添加到Arraylist,java,arraylist,Java,Arraylist,我想将文本文件数据添加到ArrayList。 这是我的文本文件 60000115 2016-10-26-05:57:47 60000104 2016-10-26-05:58:27 60000082 2016-10-26-05:58:38 60000074 2016-10-26-06:01:04 我使用空间分割数据。所以基本上有两部分。身份证和日期。 我创
60000115 2016-10-26-05:57:47
60000104 2016-10-26-05:58:27
60000082 2016-10-26-05:58:38
60000074 2016-10-26-06:01:04
我使用空间分割数据。所以基本上有两部分。身份证和日期。
我创建了一个名为Employee的bean类,其中有两个字符串,分别是user_id和mDate。
EmployerRayList是我使用Employee类创建的ArrayList。
问题是我无法向Arraylist添加值。只有最后一行数据插入到ArrayList。例如:-60000074 2016-10-26-06:01:04。
四排60000074 2016-10-26-06:01:04
这是我的密码
for (File f : files) {
FileInputStream fis;
try {
fis = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String aLine;
while ((aLine = in.readLine()) != null) {
String[] tokens = aLine.split(" ");
emp.setUser_id(tokens[0]);
emp.setmDate(tokens[1]);
employeArrayList.add(emp);
out.write(aLine);
out.newLine();
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
问题是您不断地更新同一个emp对象 您必须为每个迭代创建一个新的;再加上这个 此外:当使用需要核心属性的框架时,您应该只使用带有setters的“bean” 换句话说:您的bean类最好看起来像
public class EmplyoeeBeen {
private final String id;
private final String date;
public EmplyoeeBeen(String id, String date) {
this.id = id;
...
你在那门课上只有能手,没有二传手!而且:您可能还需要漂亮的equals/hashCode方法
最后:在命名上保持一致:不要使用“u”下划线(它们只用于常量名称);或者使用像“m”+字段名这样的前缀。。。总是或者永远不会。但是不要选择userId和mDate
但最重要的是:避免字符串化。我的意思是:ID不是字符串。日期不是日期。当你的领域具有意义时;然后使用**类来表示具有该含义的类。例如,您可以发明自己的Id类;可以肯定的是:您希望将日期字符串转换为真实的日期对象。每次都创建一个新的
Employee
对象:
while ((aLine = in.readLine()) != null) {
String[] tokens = aLine.split(" ");
Employee emp = new Employee();
emp.setUser_id(tokens[0]);
emp.setmDate(tokens[1]);
employeArrayList.add(emp);
out.write(aLine);
out.newLine();
}
原始代码的问题是您正在重用同一对象
emp
。这意味着您在列表中重复插入同一对象。在循环的每次迭代中更改该对象的字段时,会影响列表中的每个条目。通过将emp
范围限定到循环中,它将不会被回收,您的代码应该按预期工作。这是因为您没有每次创建新员工对象。按如下所示更改代码,使其不会每次使用相同的对象
for (File f : files) {
FileInputStream fis;
try {
fis = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String aLine;
while ((aLine = in.readLine()) != null) {
String[] tokens = aLine.split(" ");
Employee emp=new Employee(); //create new object of employee
emp.setUser_id(tokens[0]);
emp.setmDate(tokens[1]);
employeArrayList.add(emp);
out.write(aLine);
out.newLine();
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
请试试这个
List emp=new ArrayList();
emp.add(令牌[0],令牌[1])代码>无需每次创建标记变量。由于字符串是不可变类,您可以直接重新分配值
String[] tokens = null;
for (File f : files) {
FileInputStream fis;
try {
fis = new FileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String aLine;
while ((aLine = in.readLine()) != null) {
Employee emp = new Employee();
tokens = aLine.split(" ");
emp.setUser_id(tokens[0]);
emp.setmDate(tokens[1]);
employeArrayList.add(emp);
out.write(aLine);
out.newLine();
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
还要为每一行每次创建Employee对象
另一个最好的方法是以一种可以直接将数据保存到bean的方式保存数据。
以JSON格式保存。并使用。您可以在Bean中将用户和数据的数组用作数组列表
问题:通过更改值,您正在使用同一对象。它将反映最新的值,但该数组列表中的所有值都将指向内存中的单个对象
1.创建循环中的员工,以便您可以为每次迭代创建新对象
克隆employee对象并将其添加到列表中
您的employeeraylist
在哪里创建?第一部分;不,如果我们更新同一个对象并将其添加到ArrayList中,它确实会被添加并始终生成一个新对象。@Addy:不,当您继续添加某个对象时,您最终得到的结果是“行数”X最后一个推入该对象的内容。那不是他想要的。你应该谈谈参考原则。必须了解Java语言。