1. 程式人生 > >關於序列化物件.Serializable Object總結.

關於序列化物件.Serializable Object總結.

一個物件需要可實現序列化需要滿足以下條件.

1.實現Serializable介面

2.父類必須實現可序列化或者存在預設無參建構函式.

3.類的域變數必須實現可序列化或者定義為transient型別.

4.類內部定義 private static final long serialVersionUID = 9999L;(可選)

如果未顯式定義serialVersionUID的話,後臺會自動定義.

顯式定義serialVersionUID的好處是當物件反序列化的時候會匹配原始的serialVersionUID,如果相等則允許可反序列化

可以定義手動的序列化和反序列化.在類定義

private void readObject(ObjectInputStream in)throws ClassNotFoundException,IOException{
	in.defaultReadObject();
	//to do personal code ...
}

private void writeObject(ObjectOutputStream out)throws IOException{
	out.defaultWriteObject();
	//to do personal code ...
}

展示程式碼.
import java.io.Serializable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Person implements Serializable{
	private String name;
	private int age;
	private final static long serialVersionUID = 900L;

	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}

	public void setName(String name){
		this.name = name;
	}

	public void setAge(int age){
		this.age = age;
	}

	public String getName(){
		return name;
	}

	public int getAge(){
		return age;
	}

	private void readObject(ObjectInputStream in)throws IOException,ClassNotFoundException{
		in.defaultReadObject();
		System.out.println("read:" + this);
	}

	private void writeObject(ObjectOutputStream out)throws IOException{
		out.defaultWriteObject();
		System.out.println("write:" + this);
	}

	public String toString(){
		return String.format("name = %s,age = %d",name,age);
	}

}

import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.BufferedOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import static java.nio.file.StandardOpenOption.*;

public class SerializePerson{
	public static void main(String[] args){
		Path file = Paths.get(System.getProperty("user.dir")).resolve("file.bin");
		try{
			if(Files.exists(file)){
				Files.delete(file);
			}
		}catch(IOException e){
			e.printStackTrace();
			return;
		}

		try(ObjectOutputStream out = new ObjectOutputStream(
										new BufferedOutputStream(Files.newOutputStream(file,WRITE,CREATE)));){
			Person person = new Person("Ricky",28);
			out.writeObject(person);
			out.reset();//這裡需要呼叫reset()方法.清空ObjectOutputStream的物件記憶.否則將只記憶首個物件.
			person.setName("Jennie");
			person.setAge(27);
			out.writeObject(person);

		}catch(IOException e){
			e.printStackTrace();
			return;
		}
	}
}

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.ObjectInputStream;
import java.io.IOException;
import static java.nio.file.StandardOpenOption.*;
import java.io.EOFException;
import java.io.BufferedInputStream;

public class DeserializePerson{
	public static void main(String[] args){
		Path file = Paths.get(System.getProperty("user.dir")).resolve("file.bin");
		assert Files.exists(file);
		try(ObjectInputStream in = new ObjectInputStream(
									new BufferedInputStream(Files.newInputStream(file,READ)));){
			Person person = null;
			while(true){
				person = (Person)in.readObject();
			}
		}catch(EOFException e){
			System.out.println("EOF get end of file:" + file);
		}catch(ClassNotFoundException|IOException e){
			e.printStackTrace();
			return;
		}
	}
}