import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class ObjectOutputStream {
public static void main(String[] args) throws IOException, ClassNotFoundException{
test3();
java.io.ObjectInputStream ois = new java.io.ObjectInputStream(new FileInputStream("e.txt"));
ArrayList<Person> list = (ArrayList<Person>) ois.readObject(); //将集合对象一次读取
for (Person person : list) {
System.out.println(person);
}
ois.close();
}
private static void test3() throws IOException, FileNotFoundException {
Person p1= new Person("张三",23);
Person p2= new Person("李三",23);
Person p3= new Person("李si",23);
Person p4= new Person("王五",23);
ArrayList<Person> list = new ArrayList<>();
list.add(p4);
list.add(p3);
list.add(p2);
list.add(p1);
java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(new FileOutputStream("e.txt"));
oos.writeObject(list); //将几个对象放到集合里面,然后把这个集合一次写出
oos.close();
}
原文:https://www.cnblogs.com/wangffeng293/p/13227548.html