报错信息:
存储对象:
package IoSuper;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class ObjectStreamDemo3 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream ob=new ObjectOutputStream(new FileOutputStream("D:\\JAVA\\HeiMa\\c\\objectStream.txt"));
Student s1=new Student("zs",23);
Student s2=new Student("ls",23);
Student s3=new Student("ws",23);
ArrayList<Student> list=new ArrayList<>();
Collections.addAll(list,s1,s2,s3);
for (Student student : list) {
ob.writeObject(student);
}
ob.close();
}
}
读取对象:
package IoSuper;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class ObjectStreamDemo4 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("D:\\JAVA\\HeiMa\\c\\objectStream.txt"));
ArrayList<Student> list1 = (ArrayList<Student>) ois.readObject();
for (Student student : list1) {
System.out.println(student.getName()+" "+student.getAge());
}
ois.close();
}
}
JavaBean:
package IoSuper;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 393646513909202246L;
private String name;
private int age;
public Student(){}
public Student(String name, int age) {
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;
}
}