import java.io.*;public class Serialization {    public static void main(String [] args) {        Student stu1 = new Student(1, "Ronnie", 37, "snooker");        Student stu2 = new Student(2, "John", 37, "snooker");                try {            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(new File("student.txt")));                        os.writeObject(stu1);            os.writeObject(stu2);                        os.close();                        ObjectInputStream is = new ObjectInputStream(new FileInputStream(new File("student.txt")));                        System.out.println((Student)is.readObject());            System.out.println((Student)is.readObject());                        is.close();        } catch (Exception e) {            e.printStackTrace();        }    }}class Student implements Serializable {    int id;    String name;    int age;    String department;        public Student(int id, String name, int age, String department) {        this.id = id;        this.name = name;        this.age = age;        this.department = department;    }        public String toString() {        return "name:" + name + ", id:" + id + ", age:" + age + ", department:" + department;    }}

ObjectOutputStream和ObjectInputStream处理的对象必须实现Serializable接口