Serializable和Externalizable

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();
	}
	@Override
	public 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;
	}
	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		System.out.println("writeExternal");
		out.writeObject(name);
		out.writeInt(age);
		out.writeObject(gender);
	}
	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		System.out.println("readExternal");
		name = (String) in.readObject();
		age=in.readInt();
		gender = (String) in.readObject();
	}
	@Override
	public String toString() {
		return name+"-"+age+"-"+gender;
	}
}

發佈了56 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章