请各位帮忙这个为什么会出现这个异常?
这是异常信息
Exception in thread "main" [Person [name=张三, age=23], Person [name=李四, age=24], Person [name=王五, age=25]]
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at byron.darkhorse.day22.Demo_ObjectInputStream.main(Demo_ObjectInputStream.java:15)
下面我帖出代码,麻烦大神帮忙分析下,谢谢。
public class Demo_ObjectOutputStream {
public static void main(String[] args) throws FileNotFoundException, IOException {
Person p1 = new Person("张三",23);
Person p2 = new Person("李四",24);
Person p3 = new Person("王五",25);
ArrayList<Person> list = new ArrayList<>();
list.add(p1);
list.add(p2);
list.add(p3);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
oos.writeObject(list);
oos.close();
}
}
public class Demo_ObjectInputStream {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"));
System.out.println(ois.readObject());
ArrayList<Person> list = (ArrayList<Person>) ois.readObject();
for (Person person : list) {
System.out.println(person);
}
ois.close();
}
}
public class Person implements Serializable{
/**
*
*/
private static final long serialVersionUID = 2L;
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
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 "Person [name=" + name + ", age=" + age + "]";
}
}
|
|