很抱歉今天发布了关于序列化的第二篇文章,修复一个问题导致了另一个问题。
如此处所述 - Java - Serialization - NotSerializableException Issue - 我有一个包含以下类(class)的项目
Student.java
StudentsCollection.java
Students 创建我的 Student 对象(不言自明),我的 StudentsCollection() 实例化一个存储我的 Student 对象的 Student 类型列表,当尝试保存/加载对象时,我使用此代码并抛出以下异常:
/**
* Open student collection
*/
public void openCollection(){
try {
FileInputStream e = new FileInputStream("students.ser");
ObjectInputStream inputSteam = new ObjectInputStream(e);
while(inputSteam.readObject() != null){
this.list.add((Students)inputSteam.readObject());
}
} catch (FileNotFoundException var3) {
var3.printStackTrace();
JOptionPane.showMessageDialog(null, "File not found");
} catch (IOException var4) {
var4.printStackTrace();
JOptionPane.showMessageDialog(null, "IO Exception");
} catch (ClassNotFoundException var5) {
var5.printStackTrace();
JOptionPane.showMessageDialog(null, "Required class not found");
}
}
并且正在抛出:
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2598)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1318)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at jdatabase.objects.students.StudentsCollection.openCollection(StudentsCollection.java:558)
at jdatabase.main.MainController.main(MainController.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
我只在我的列表中添加了一个 Student 对象,保存列表并重新打开它后,我要求控制台打印出该列表,Student 实际上打印了出来。然而,当我创建多个学生对象并每次将其 ID 加 1 并添加它们时,控制台会按顺序打印所有这些对象,然后重新打印(由于某些奇怪的原因)并最终跳过一些。
如果您需要更多代码,请询问。 saveCollection() 现在工作正常
修改后的代码: /**
* Open student collection
*/
public void openCollection(){
try {
FileInputStream e = new FileInputStream("students.ser");
ObjectInputStream inputSteam = new ObjectInputStream(e);
while((obj = inputSteam.readObject()) != null){
this.list.add((Students)obj);
}
} catch (FileNotFoundException var3) {
var3.printStackTrace();
JOptionPane.showMessageDialog(null, "File not found");
} catch (IOException var4) {
var4.printStackTrace();
JOptionPane.showMessageDialog(null, "IO Exception");
} catch (ClassNotFoundException var5) {
var5.printStackTrace();
JOptionPane.showMessageDialog(null, "Required class not found");
}
}
抛出:
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2598)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1318)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at jdatabase.objects.students.StudentsCollection.openCollection(StudentsCollection.java:559)
at jdatabase.main.MainController.main(MainController.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
只有 1 个学生对象,控制台中打印以下内容:
Student name: Student Student surname: Default Student ID: 0 Student DoB: 1/1/90
并再次打印:
Student name: Student Student surname: Default Student ID: 0 Student DoB: 1/1/90
最佳答案
readObject()
在流末尾不会返回 null
,因此对其进行测试,因为循环终止条件无效。当遇到流结尾时,您的代码正确抛出EOFException
。这里没有需要解决的问题。
关于Java - 序列化 - EOFException 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24825958/