我有一个员工条目的文本文件,如下所示:
000, first name1, middle name1
001, first name2, middle name2
002, first name3, middle name3
003, first name4, middle name4
004, first name5, middle name5
我有一个班级员工如下:
public class Employee {
public int id;
public String fName;
public String mName;
public String lName;
}
我已将文件内容读入数组。但我想要的是构造一个Employee类的对象数组,以及用每个条目初始化类的每个属性的方法。像这样:
Employee e[] = new Employee[5];
检查数组中每个对象的详细信息......
e[0].id = 000
e[0].fName = "first name1"
e[0].mName = "middle name1"
e[0].lName = "last name1"
然后,
e[1].id = 001
等等......
我有什么方法可以做到这一点吗?
答案 0 :(得分:2)
public class Employee {
public int id;
public String fName;
public String mName;
public String lName;
public Employee(String line) {
String[] split = line.split(",");
id = Integer.parseInt(split[0]);
fName = split[1];
mName = split[2];
lName = split[3];
}
}
因为您已经将文件读入数组(我想是字符串)。
String[] lines = ....;
Employee[] employees = new Employee[lines.length];
for(int i = 0; i < lines.length; i++) {
employees[i] = new Employee(lines[i]);
}
你去......你有一系列的员工。
答案 1 :(得分:1)
读取文件并逐行循环遍历其内容。然后解析这些行并在每次迭代中创建一个new Employee()
。设置您的值,例如id和name。最后,将新的Employee
实例添加到List<Employee>
并继续下一个条目。
// Read data from file
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
// List to collect Employee objects
List<Employee> employees = new ArrayList<Employee>();
// Read file line by line
String line = "";
while ((line = br.readLine()) != null) {
// Parse line to extract individual fields
String[] data = this.parseLine(line);
// Create new Employee object
Employee employee = new Employee();
employee.id = Integer.valueOf(data[0]);
employee.fName = data[1];
employee.mName = data[2];
// Add object to list
employees.add(employee);
}
// Further process your Employee objects...
}
此外,还有一些CSV库可以处理读取具有逗号分隔值的文件的所有令人讨厌的部分。我建议使用OpenCSV,例如。
答案 2 :(得分:0)
这里已经发布了答案,但我还是要发帖...
package com.stackoverflow.java.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Employee {
public int id;
public String fName;
public String mName;
public Employee(int id, String fName, String mName) {
this.id = id;
this.fName = fName;
this.mName = mName;
}
public static void main(String[] args) throws IOException {
Employee[] e = new Employee[5];
FileReader fr=new FileReader("YourDoc.txt");
BufferedReader br=new BufferedReader(fr);
String line="";
String[] arrs=null;
int num=0;
while ((line=br.readLine())!=null) {
arrs=line.split(",");
e[num] = new Employee(Integer.valueOf(arrs[0]), arrs[1], arrs[2]);
num++;
}
br.close();
fr.close();
for(int i=0 ; i< e.length; i++) {
System.out.println(e[i].id + " and " + e[i].fName + " and " + e[i].mName);
}
}
}
答案 3 :(得分:0)
尝试:
public static void main(String[] args) throws IOException {
File f1 = new File("d:\\data.txt");
Scanner scanner = new Scanner(f1);
List<Employee1> empList=new ArrayList<>();
while(scanner.hasNextLine()){
String data[]=scanner.nextLine().split(",");
empList.add(new Employee(Integer.parseInt(data[0]),data[1],data[2],data[3]));
}
scanner.close();
System.out.println(empList);
}
Employee.java
class Employee{
public int id;
public String fName;
public String mName;
public String lName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public Employee(int id, String fName, String mName, String lName) {
super();
this.id = id;
this.fName = fName;
this.mName = mName;
this.lName = lName;
}
@Override
public String toString() {
return "Employee [id=" + id + ", fName=" + fName + ", mName="
+ mName + ", lName=" + lName + "]";
}
}