/**
* @param args
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException,
IOException, ClassNotFoundException {
// Demo1();
// Demo2();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"fff.txt"));
ArrayList<Person> list = (ArrayList<Person>) ois.readObject();
for (Person person : list) {
System.out.println(person);
}
ois.close();
}
private static void Demo2() throws IOException, FileNotFoundException {
Person p1 = new Person("张三", 23);
Person p2 = new Person("李四", 24);
Person p3 = new Person("王五", 24);
Person p4 = new Person("赵六", 24);
ArrayList<Person> list = new ArrayList<>();
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"fff.txt"));
oos.writeObject(list);
oos.close();
}
private static void Demo1() throws IOException, FileNotFoundException,
ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"aaa.txt"));
Person p1 = (Person) ois.readObject();
Person p2 = (Person) ois.readObject();
// Person p3 = (Person) ois.readObject();
// //当读到末尾时会出现EOFException(文件末尾异常)
System.out.println(p1);
System.out.println(p2);
ois.close();
}