對象序列化
對象序列化
對象序列化定義
所謂的對象序列化就是将 儲存在記憶體中的對象資料轉換為二進制資料流進行傳輸的操作 ;但不是所有對象都可以進行序列化,要被序列化的的對象那麼其所在的類一定要實作 java.io.Serializable 接口,該接口并沒有認識的操作方法,因為該接口是一個 辨別接口 。
可以被序列化的類
import java.io.Serializable;
@SuppressWarnings("serial")
class Book implements Serializable {
private String title ;
private double price ;
public Book(String title , double price) {
this.price = price ;
this.title = title ;
}
public String toString() {
return "this.title + "\t" + this.price";
}
}
如上,就實作了一個可以被序列化的類,(使用了壓制警告)。
如此,Book類就可以實作二進制的傳輸了!
實作序列化和反序列化
序列化類:
java.io.ObjectOutputStream
将對象轉為指定格式的二進制資料
構造方法:
public ObjectOutputStream(OutputStream out)
輸出對象:
public final void writeObject(Object obj)
反序列化類:
java.io.ObjectInputStream
将已經序列化的對象轉換回原本的對象内容
構造方法:
public ObjectInputStream(InputStream in)
讀取對象:
public final Object readObject()
實作序列化對象操作
@SuppressWarnings("serial")
class Book implements Serializable {
private String title ;
private double price ;
public Book(String title , double price) {
this.price = price ;
this.title = title ;
}
public String toString() {
return this.title + "\t" + this.price ;
}
}
public class TestDemo {
public static void main(String [] args) throws Exception {
ser();
}
public static void ser() throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));
oos.writeObject(new Book("Java開發",110.1));
oos.close();
}
}
實作反序列化類
public static void dser() throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream (new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));
Object obj = ois.readObject();// 按照Object讀取
Book book = (Book) obj;// 向下轉型
System.out.println(book);
ois.close();
}
序列化與反序列化完整實作
package helloworld;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
@SuppressWarnings("serial")
class Book implements Serializable {
private String title ;
private double price ;
public Book(String title , double price) {
this.price = price ;
this.title = title ;
}
public String toString() {
return this.title + "\t" + this.price ;
}
}
public class TestDemo {
public static void main(String [] args) throws Exception {
ser(); // 實作序列化
dser(); // 實作反序列化
}
public static void ser() throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));
oos.writeObject(new Book("Java開發",110.1));// 輸出
oos.close();
}
public static void dser() throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream (new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));
Object obj = ois.readObject();// 按照Object讀取
Book book = (Book) obj;// 向下轉型
System.out.println(book);
ois.close();
}
}
transient 關鍵字
通過序列化和反序列化的code實作,我們發現:序列化操作時是将整個對象的所有屬性内容進行儲存;但是如果某些屬性的内容不需要被儲存就可以通過 transient 關鍵字定義。
private transient String title;
由定義可知,title屬性不可以被序列化操作。
總結
不是所有的類都需要被序列化,隻有需要傳輸的對象所在的類才需要序列化對象。