假设我们在序列化中,对一个对象进行多次序列化,反序列化是会是单个还是多个对象呢?先来看一个例子。
public class Pet implements Serializable {
private String name;
public Pet(String name) {
this.name = name;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
//构建序列化集合
ArrayList<Pet> pets = new ArrayList<>();
pets.add(new Pet("dog"));
pets.add(new Pet("cat"));
pets.add(new Pet("pig"));
System.out.println("------打印初始集合------");
System.out.println(pets);
//第一次构建输出流
ByteArrayOutputStream bout1 = new ByteArrayOutputStream();
ObjectOutputStream out1 = new ObjectOutputStream(bout1);
//第一次写入集合
out1.writeObject