最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java序列化(Serialization) 機制

 更新時間:2016年07月07日 11:28:56   投稿:lqh  
本篇文章是對Java中對象的序列化(Serialization) 機制進行了詳細的分析介紹,并附實例,需要的朋友可以參考下

  Java中,一切都是對象,在分布式環(huán)境中經(jīng)常需要將Object從這一端網(wǎng)絡(luò)或設(shè)備傳遞到另一端。這就需要有一種可以在兩端傳輸數(shù)據(jù)的協(xié)議。Java序列化機制就是為了解決這個問題而產(chǎn)生。

將對象狀態(tài)轉(zhuǎn)換成字節(jié)流之后,可以用java.io包中各種字節(jié)流的類將其保存到文件中,管道到另一線程中或通過網(wǎng)絡(luò)連接將對象數(shù)據(jù)發(fā)送到另一主機。對象序列化功能非常簡單、強大,在RMI、Socket、JMS、EJB都有應(yīng)用。對象序列化問題在網(wǎng)絡(luò)編程中并不是最核心的課題,但卻相當重要,具有許多實用意義。

java對象序列化不僅保留一個對象的數(shù)據(jù),而且遞歸保存對象引用的每個對象的數(shù)據(jù)。可以將整個對象層次寫入字節(jié)流中,可以保存在文件中或在網(wǎng)絡(luò)連接上傳遞。利用對象序列化可以進行對象的“深復(fù)制”,即復(fù)制對象本身及引用的對象本身。序列化一個對象可能得到整個對象的序列。

基本使用方法:  

      Serialization是指把類或者基本的數(shù)據(jù)類型持久化(persistence)到數(shù)據(jù)流(Stream)中,包括文件、字節(jié)流、網(wǎng)絡(luò)數(shù)據(jù)流。

JAVA中實現(xiàn)serialization主要靠兩個類:ObjectOuputStream和ObjectInputStream。他們是JAVA IO系統(tǒng)里的OutputStream和InputStream的子類。既然他們是JAVA IO中的流,那么就可以像操作一般的流一樣來操作他們。下面是他們使用方法:

import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 
 
public class Pair implements Serializable{ 
 
 private static final long serialVersionUID = -1874850715617681161L; 
 private int type; 
 private String name; 
 
 public int getType() { 
 return type; 
 } 
 
 public void setType(int type) { 
 this.type = type; 
 } 
 
 public String getName() { 
 return name; 
 } 
 
 public void setName(String name) { 
 this.name = name; 
 } 
 
 
 public Pair(int type, String name) { 
 super(); 
 this.type = type; 
 this.name = name; 
 } 
 
 public static void main(String[] args) throws IOException, ClassNotFoundException { 
 // TODO Auto-generated method stub 
 //serialize object pair 
 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
 ObjectOutputStream oos = new ObjectOutputStream(bos); 
 Pair pair = new Pair(1, "charlie"); 
 oos.writeObject(pair); 
 //deserialize object, get new object newpair 
 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 
 ObjectInputStream ois = new ObjectInputStream(bis); 
 Pair newpair = (Pair) ois.readObject(); 
 
 System.out.println(newpair.getType()+":"+newpair.getName()); 
 } 
} 

1. 這兩個類都是decorator模式的,在創(chuàng)建他們的時候,都要傳入一個基于字節(jié)的流,真正在底下存貯序列化數(shù)據(jù)的都是這些流。

 2. 被持久化的類要實現(xiàn)Serializable接口,這個接口沒有任何函數(shù),只是一個標記接口。如果在一臺機器上進行序列化,把得到的數(shù)據(jù)傳送到另外一個機器上進行反序列化,那么這兩臺機器上的類應(yīng)該是完全一樣的,否則序列化是不會成功的。

 3. 切記不要把上面代碼中的bos用toString得到String,然后再從這個String中得到ByteArrayInputStream,再進行反序列化。bos是以字節(jié)存貯的,轉(zhuǎn)成以字符存貯的String必然會造成數(shù)據(jù)的變化,而從String中到的byte[]也不會是之前那個byte[]了。我遇到過這個問題,是因為我想把序列化之后的數(shù)據(jù)存在xml文件中。這個問題的具體解決方法見我的另外一篇文章:

www.fzitv.net/article/88130.htm

java虛擬機在序列化和反序列化的時候都做了些什么?

javadoc中對這兩個類的描述中對java的序列化機制進行了詳細的描述:

引用

The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.

默認的序列化機制寫到流中的數(shù)據(jù)有:

1、對象所屬的類
2、類的簽名
3、所有的非transient和非static的屬性
4、對其他對象的引用也會造成對這些對象的序列化
5、如果多個引用指向一個對象,那么會使用sharing reference機制

引用

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

private void readObject(java.io.ObjectInputStream stream) 
 throws IOException, ClassNotFoundException; 
private void writeObject(java.io.ObjectOutputStream stream) 
 throws IOException 
private void readObjectNoData() 
 throws ObjectStreamException; 

相關(guān)文章

最新評論

阿城市| 靖远县| 江都市| 晋中市| 广元市| 武隆县| 渭源县| 霍城县| 宜川县| 务川| 玛纳斯县| 黑龙江省| 天全县| 沐川县| 新建县| 礼泉县| 城固县| 博爱县| 鹤岗市| 若羌县| 西华县| 鞍山市| 福贡县| 萝北县| 凤翔县| 二连浩特市| 双峰县| 衢州市| 凤庆县| 张北县| 岱山县| 石渠县| 化隆| 北宁市| 平潭县| 凉山| 洞头县| 来凤县| 筠连县| 宁陕县| 葵青区|