程序报了一个错误: Exception in thread "main" java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2596) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1316) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370) at ObjectStreamTest.read(ObjectStreamTest.java:27) at ObjectStreamTest.main(ObjectStreamTest.java:14)
请问怎么解决? 数据可以正常输出。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/*
* 需求:通过序列化流把三个对象写入文本文件,并遍历。
*/
public class ObjectStreamTest {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
read();
}
public class Person implements Serializable {
// 产生一个固定的序列号id值
// private static final long serialVersionUID = -6838499878718774345L;
private String name;
private int age;
// public int age;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "name:" + name + ",age:" + age;
}
}