package object;
import java.io.*;
import java.util.ArrayList;
public class ConvertedDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student stu1 = new Student("张三",23);
Student stu2 = new Student("李四",24);
Student stu3 = new Student("王五",25);
ArrayList list = new ArrayList<>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
// 用一个学生类的集合装数据
ObjectOutputStream ops =new ObjectOutputStream(new FileOutputStream("charstream\student.txt"));
// ObjectOutputStream是操作对象数据的
ops.writeObject(list);
// 将这个学生类的集合写入文件
ops.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("charstream\student.txt"));
// 将读取的数据强转成一个学生类的集合
ArrayList ol = (ArrayList )ois.readObject();
for (Student student : ol) {
System.out.println(student);
}
ois.close();
}
}