我正在尝试创建一些与它从文件中读取的行数相同的 Employee 对象
I am trying to create a number of Employee objects same as the number of line it reads from the file
行列表
私有 ArrayList 行;
list of Employee objects
private ArrayList<Employee> employeeList;
the file information of employee
private FileReader file;
public HumanResource(String fileName)
{
file = new FileReader(fileName);
ArrayList<Employee> employeeList = new ArrayList<>();
for (int k = 0; k < file.getLines().size(); k++)
{
employeeList.add(new Employee (file.getLines().get(k)));
}
}
这是我到目前为止的想法,但我不知道这是否正确?
一个更好的方法是使用 BufferReader 并且你可以阅读每一行。
try(BufferedReader br = new BufferedReader(new FileReader(fileName))){
string st;
ArrayList<Employee> employeeList = new ArrayList<>();
while ((st = br.readLine()) != null)
employeeList.add(new Employee(st));
}
}
请注意,我正在使用 try{} 扭曲 bufferedReader 以使其自动关闭
行列表 私有 ArrayList 行;
list of Employee objects
private ArrayList<Employee> employeeList;
the file information of employee
private FileReader file;
public HumanResource(String fileName)
{
file = new FileReader(fileName);
ArrayList<Employee> employeeList = new ArrayList<>();
for (int k = 0; k < file.getLines().size(); k++)
{
employeeList.add(new Employee (file.getLines().get(k)));
}
}
这是我到目前为止的想法,但我不知道这是否正确?
一个更好的方法是使用 BufferReader 并且你可以阅读每一行。
try(BufferedReader br = new BufferedReader(new FileReader(fileName))){
string st;
ArrayList<Employee> employeeList = new ArrayList<>();
while ((st = br.readLine()) != null)
employeeList.add(new Employee(st));
}
}
请注意,我正在使用 try{} 扭曲 bufferedReader 以使其自动关闭