我正在尝试使用 ObjectOutputStream 将 Arraylist 中的所有对象保存到文件中。对象的一个属性是 LocalDate,每当我尝试写入文件时,都会出现错误 NotSerializableException: java.time.format.DateTimeFormatter returns despite having no DateTimeFormatter for any of the LocalDates
完整错误:
java.io.NotSerializableException: java.time.format.DateTimeFormatter
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1185)
at java.base/java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1553)
at java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1510)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1433)
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1179)
at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
at java.base/java.util.ArrayList.writeObject(ArrayList.java:791)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1130)
at java.base/java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1497)
at java.base/java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1433)
at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1179)
at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
at BikeNow.saveRent(BikeNow.java:330)
at BikeNow.main(BikeNow.java:114)
使用Object输出Stream的方法
public void saveRent() {
//Create file and object output stream
try {
FileOutputStream fos = new FileOutputStream("tmp.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//Write array to file
oos.writeObject(rents);
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
试图将对象保存到文件的示例
rents.add(new Rent(0001, "John Smith", true, "Roubaix Sport", LocalDate.of(2019, 03, 06), LocalDate.of(2019, 04, 05), 30, true));
项目等级
import java.io.Serializable;
import java.time.LocalDate;
public class Rent extends Customer implements Serializable {
private LocalDate startDate;
private LocalDate endDate;
private int duration;
private boolean overdue;
public Rent(int customerID, String customerName, boolean renting, String bikeRented, LocalDate startDate, LocalDate endDate, int duration, boolean overdue) {
super(customerID, customerName, renting, bikeRented);
this.startDate = startDate;
this.endDate = endDate;
this.duration = duration;
this.overdue = overdue;
}
最佳答案
这里没有太多内容可以继续,但显然您在编写对象时试图编写 DateTimeFormatter。这让我相信在 Customer 中定义了一个,但由于 DTF 没有实现 Serializable,它爆炸了。
最好的解决方案是编辑您的 Customer 类以正确序列化。另一种选择是将其填充到您的 Rent 类中,但如果字段是私有(private)的,这可能是不可能的。
关于java - 如何修复 'NotSerializableException: java.time.format.DateTimeFormatter' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55584072/