本文转自http://blog.sina.com.cn/s/blog_77cb34170100r5o6.html
1.
ObjectInputStream与ObjectOutputStream类所读写的对象必须实现Serializable接口,对象中的transient和static类型成员变量不会被读取和写入
2.
import java.io.*;
public class serializtion {
public static void main(String[] args)throws IOException{
Student s1=new Student("张三", 1, 15, "化学");
Student s2=new Student("李四", 2, 19, "生物");
FileOutputStream fout=new FileOutputStream("student.txt");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(s1);
out.writeObject(s2);
out.close();
FileInputStream fin=new FileInputStream("student.txt");
ObjectInputStream in=new ObjectInputStream(fin);
try {
s1=(Student) in.readObject();
s2=(Student) in.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
in.close();
System.out.print("name:"+s1.name);
System.out.print(" id:"+s1.id);
System.out.print(" age:"+s1.age);
System.out.println(" department:"+s1.department);
System.out.print("name:"+s2.name);
System.out.print(" id:"+s2.id);
System.out.print(" age:"+s2.age);
System.out.println(" department:"+s2.department);
}
}
3.
import java.io.*;
public class Student implements Serializable {
String name;
int id ;
int age;
String department;
public Student(String name, int id, int age, String department) {
this.age = age;
this.department = department;
this.id = id;
this.name = name;
}
}