Serializable和Externalizable

来源:互联网 发布:浙江师范行知学院官网 编辑:程序博客网 时间:2024/06/07 07:10

1、Serializable自动序列化

public class Client1 {public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {Student s = new Student(1, "Simon", "music");ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.txt"));oos.writeObject(s);oos.close();s = null;ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.txt"));s = (Student) ois.readObject();ois.close();System.out.println("student info:"+s.getId()+"-"+s.getName()+"-"+s.getMajor());}}class Student implements Serializable{private static final long serialVersionUID = 1L;private int id;private String name;private transient String major;//自动序列化,major不被序列化public Student() {super();}public Student(int id, String name, String major) {super();this.id = id;this.name = name;this.major = major;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getMajor() {return major;}public void setMajor(String major) {this.major = major;}}

2、Serializable手动序列化

public class Client3 {public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {House h = new House("yellow", 15f);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("girl.out"));oos.writeObject(h);oos.close();h = null;ObjectInputStream ois = new ObjectInputStream(new FileInputStream("girl.out"));h = (House) ois.readObject();ois.close();System.out.println(h);}}class House implements Serializable{private static final long serialVersionUID = 1350639853941243177L;private transient String color;//手动序列化private float size;public House(String color, float size) {super();this.color = color;this.size = size;}private void writeObject(ObjectOutputStream out)     throws IOException{System.out.println("writeObject");out.writeObject(color);out.writeFloat(size);}private void readObject(ObjectInputStream in)throws IOException, ClassNotFoundException{System.out.println("readObject");color = (String) in.readObject();size = in.readFloat();}@Overridepublic String toString() {System.out.println("toString");return color+"-"+size;}}

3、Externalizable可外部化

public class Client2 {public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {List<Person> list = new ArrayList<>();Person p = new Person("Sarah", 18, "female");Person p2 = new Person("Simon", 20, "male");list.add(p);list.add(p2);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.out"));oos.writeObject(list);oos.close();list = null;ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.out"));System.out.println("before read");list = (List<Person>) ois.readObject();ois.close();System.out.println(list.get(0)+"\n"+list.get(1));}}class Person implements Externalizable{private String name;private int age;private String gender;//反序列化要用空参构造//若无空参构造,则报InvalidClassException异常public Person() {super();System.out.println("Person()");}public Person(String name, int age, String gender) {super();this.name = name;this.age = age;this.gender = gender;}@Overridepublic void writeExternal(ObjectOutput out) throws IOException {System.out.println("writeExternal");out.writeObject(name);out.writeInt(age);out.writeObject(gender);}@Overridepublic void readExternal(ObjectInput in) throws IOException,ClassNotFoundException {System.out.println("readExternal");name = (String) in.readObject();age=in.readInt();gender = (String) in.readObject();}@Overridepublic String toString() {return name+"-"+age+"-"+gender;}}

0 0