import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
public int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.io.*;
import java.util.ArrayList;
public class Demo34Class {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ArrayList<Person> list = new ArrayList<>();
list.add(new Person("张三",18));
list.add(new Person("李四",19));
list.add(new Person("王五",20));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("D:\\software\\javadome\\file\\test7.txt"),true));
oos.writeObject(list);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("D:\\software\\javadome\\file\\test7.txt")));
Object o = ois.readObject();
ArrayList<Person> list2 = (ArrayList<Person>)o;
for (Person p : list2) {
System.out.println(p);
}
ois.close();
oos.close();
}
}