[英]Java write and read an array of objects to file
我有一个名为Train
的基类:
public abstract class Train {
String serialNo;
float weight;
final String label;
public Train(String serialNo, float weight, String label) {
this.serialNo = serialNo;
this.weight = weight;
this.label = label;
}
public abstract float getCapacity();
}
我有两个实现抽象类的类(代码中将不包括getter和setter):
public class FreightTrain extends Train implements Cloneable {
private float capacityKg;
Vector<String> freightVector = new Vector<String>();
public FreightTrain(String serialNo, float weight, String label) throws Exception{
super(serialNo, weight, label);
if (weight < 0)
throw new Exception();
}
@Override
public float getCapacity() {
return this.capacityKg;
}
}
和PassengerTrain类:
public class PassengerTrain extends Train implements Cloneable {
private float seatNo;
Vector<String> passengerId = new Vector<String>();
public PassengerTrain(String serialNo, float weight, String label) throws Exception{
super(serialNo, weight, label);
if (weight < 0)
throw new Exception();
}
@Override
public float getCapacity() {
return this.seatNo;
}
}
接下来,我有一个数组列表ArrayList<Train> arr;
其中包含: PassengerTrain
和FreightTrain
。 我想创建一些方法来将项目从arr
写入文件并从文件读取数据
这是我的尝试:
public void writeObjectInTextFile(ArrayList<Train> array) {
Formatter f = null;
try {
f = new Formatter(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for(Train train: array) {
if(train instanceof PassengerTrain) {
String label = train.getLabel();
String serialNo = train.getSerialNo();
float capacity = train.getCapacity();
float weight = train.getWeight();
float seatNo = ((PassengerTrain) train).getSeatNo();
String passId = "";
for(String id:((PassengerTrain) train).getPassengerId()) {
passId += id;
}
f.format("%s %f %s %f %f %s", serialNo, weight, label, capacity, seatNo, passId);
} else if(train instanceof FreightTrain) {
String label = train.getLabel();
String serialNo = train.getSerialNo();
float capacity = train.getCapacity();
float weight = train.getWeight();
float capacityKg = ((FreightTrain) train).getCapacityKg();
String freightVector = "";
for(String freight: ((FreightTrain) train).getFreightVector()) {
freightVector += freight;
}
f.format("%s %f %s %f %f %s", serialNo, weight, label, capacityKg, capacity, freightVector);
}
}
f.close();
}
但是我有一个问题:我无法创建一个函数来从文件中读取此数据,并返回带有初始数据的正确ArrayList。
将两个从单个类派生到文件的两个不同类的数组写入的最佳和最快方法是什么?
以及如何重新创建它?
谢谢!
请不要重复我的问题。 我搜索了类似的问题,但是我的问题与可用的问题有所不同。
我不知道如何将对象从文件转换回各自的类型。 如果我有n
派生类怎么办?
只需让您的类实现Serializable ,即可随时将对象写入文件(当然,并读回它们)! 这也包括对象的数组。
那就是上面10000英尺的答案。 正确地实现Serializable(以一种不会被人咬到的方式)是一个令人生畏的课题。 但是,有很多文献可以教您如何做以及如何避免常见的陷阱。 我个人为此推荐Joshua Bloch的“有效Java”。 他为此主题专门整整一两章。
if(train instanceof PassengerTrain)
这很不好。 您完全放弃了继承和多态的全部目的。 您的代码不必在乎火车的类型。
您应该使用Java的对象序列化功能。 在Train
类中实现Serializable
,并使用ObjectOutputStream
编写和使用ObjectInputStream
读取对象。
写:
public void writeTrainsToFile(ArrayList<Train> array, String file) {
FileOutputStream fileOutputStream = new FileOutputStream(file);
try(ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)){
objectOutputStream.writeInt(array.size());
for(Train train: array) {
objectOutputStream.writeObject(train);
}
}
}
读:
public void readTrainsFromFile(ArrayList<Train> array, String file) {
FileInputStream fileInputStream = new FileInputStream(file);
try(ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)){
int trainCount = objectInputStream.readInt();
for (int i = 0; i < trainCount; i++) {
array.add((Train)objectInputStream.readObject());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.