Java 如何从文件读入对象(构造函数)
所以我得到了一个名为Java 如何从文件读入对象(构造函数),java,Java,所以我得到了一个名为Car的类,在那里我存储了与汽车相关的信息。 看起来像这样 public class Car { private String reg; private String model; private int year; private String name; private String date; private int millage; private String service; //Constructo
Car
的类,在那里我存储了与汽车相关的信息。
看起来像这样
public class Car {
private String reg;
private String model;
private int year;
private String name;
private String date;
private int millage;
private String service;
//Constructor for the Car Class
public Car (String reg, String model,int year, String name, String date, int millage, String service){
this.reg = reg;
this.millage = millage;
this.date = date;
this.service = service;
this.name = name;
this.model = model;
this.year = year;
}
用户输入有关汽车的信息后,所有信息都存储在一个数组列表中,数组列表中的数据被打印到一个.txt文件中存储
到目前为止文本文件的外观如何
Ford-ERI56Y-2003-Tuva Larsson-2020/24/01-Large Service
Volvo -JDV810-1999-Åsa Eriksson-2020/24/01-Small Service
我希望能够扫描文本文件,并将文件内容添加回ArrayList
,以便以后使用
到目前为止我有这个代码
BufferedReader br = new BufferedReader(new FileReader(file));
String read;
while ((read = br.readLine()) != null) {
String [] carParts = read.split("-");
carList.add(read)
}
}
这不起作用,因为ArrayList
使用Car
而不是String
ArrayList<Car> carList = new ArrayList<>();
ArrayList carList=new ArrayList();
对您的汽车
类进行了一些更改,因此现在看起来如下所示:
import java.time.LocalDate;
public class Car {
private final String registration;
public String getRegistration() { return registration; }
private final String model;
public String getModel() { return model; }
private final Integer year;
public Integer getYear() { return year; }
private final String name;
public String getName() { return name; }
private final LocalDate date;
public LocalDate getDate() { return date; }
private final Integer mileage;
public Integer getMileage() { return mileage; }
private final String service;
public String getService() { return service; }
public Car(String registration, String model, Integer year, String name, LocalDate date, Integer mileage,
String service) {
this.registration = registration;
this.model = model;
this.year = year;
this.name = name;
this.date = date;
this.mileage = mileage;
this.service = service;
}
@Override
public String toString() {
return "Car{" +
"registration='" + registration + '\'' +
", model='" + model + '\'' +
", year=" + year +
", name='" + name + '\'' +
", date=" + date +
", mileage=" + mileage +
", service='" + service + '\'' +
'}';
}
}
另外,包含条目的example.txt文件缺少几列,因此我继续添加这些内容:
ERI56Y-Ford-2003-Tuva Larsson-2020/24/01-60000-Large Service
JDV810-Volvo-1999-Åsa Eriksson-2020/24/01-750000-Small Service`
现在pojo和文本文件都已确定,从中读取并创建汽车列表非常容易。检查以下示例:
private static List<Car> readFromFile(String filePath) {
try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
return lines
.map(line -> line.split("-"))
.map(parts -> new Car(parts[0], parts[1],
Integer.valueOf(parts[2]), parts[3],
LocalDate.parse(parts[4], DateTimeFormatter.ofPattern("yyyy/dd/MM")),
Integer.valueOf(parts[5]), parts[6]))
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
return Collections.emptyList();
}
您需要通过调用其构造函数并传递carParts数组中所需的参数来创建一个Car。然后您需要将该车添加到列表中:
car car=new car(carParts[0],…);添加(汽车)代码>。
public static void main(String... args) {
readFromFile(System.getProperty("user.home") + "/cars.txt").forEach(System.out::println);
}