序列化集合–序列化流演示
package com.zr;
import java.io.*;
import java.util.ArrayList;
/*
序列化集合--序列化流演示
*/
public class DemoSerialization {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建ArrayList集合,存储Person对象
ArrayList<Person> list=new ArrayList<>();
//添加Person对象
list.add(new Person("a",1));
list.add(new Person("b",2));
list.add(new Person("c",3));
//创建对象序列化输出流
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("list.txt"));
//写入数据
oos.writeObject(list);
//创建对象反序列化输入流
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("list.txt"));
//创建新的list集合接收反序列化获得的数据
ArrayList<Person> list2= (ArrayList<Person>) ois.readObject();
//输出新的集合
System.out.println(list2);
//释放资源
ois.close();
oos.close();
}
}