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

Java序列化(Serialization) 機(jī)制

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

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

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

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

基本使用方法:  

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

JAVA中實(shí)現(xiàn)serialization主要靠?jī)蓚€(gè)類(lèi):ObjectOuputStream和ObjectInputStream。他們是JAVA IO系統(tǒng)里的OutputStream和InputStream的子類(lèi)。既然他們是JAVA IO中的流,那么就可以像操作一般的流一樣來(lái)操作他們。下面是他們使用方法:

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. 這兩個(gè)類(lèi)都是decorator模式的,在創(chuàng)建他們的時(shí)候,都要傳入一個(gè)基于字節(jié)的流,真正在底下存貯序列化數(shù)據(jù)的都是這些流。

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

 3. 切記不要把上面代碼中的bos用toString得到String,然后再?gòu)倪@個(gè)String中得到ByteArrayInputStream,再進(jìn)行反序列化。bos是以字節(jié)存貯的,轉(zhuǎn)成以字符存貯的String必然會(huì)造成數(shù)據(jù)的變化,而從String中到的byte[]也不會(huì)是之前那個(gè)byte[]了。我遇到過(guò)這個(gè)問(wèn)題,是因?yàn)槲蚁氚研蛄谢蟮臄?shù)據(jù)存在xml文件中。這個(gè)問(wèn)題的具體解決方法見(jiàn)我的另外一篇文章:

m.fzitv.net/article/88130.htm

java虛擬機(jī)在序列化和反序列化的時(shí)候都做了些什么?

javadoc中對(duì)這兩個(gè)類(lèi)的描述中對(duì)java的序列化機(jī)制進(jìn)行了詳細(xì)的描述:

引用

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.

默認(rèn)的序列化機(jī)制寫(xiě)到流中的數(shù)據(jù)有:

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

引用

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)文章

最新評(píng)論

进贤县| 同江市| 乌拉特中旗| 穆棱市| 阿克苏市| 无为县| 云梦县| 玛纳斯县| 呼图壁县| 图们市| 巴塘县| 嫩江县| 和林格尔县| 红原县| 苍南县| 尉氏县| 尉犁县| 松潘县| 恩施市| 同江市| 宣城市| 霍林郭勒市| 龙游县| 迁安市| 江达县| 安阳市| 盐城市| 荃湾区| 东方市| 隆化县| 四川省| 温州市| 喀喇| 临夏县| 江津市| 黄浦区| 洛浦县| 邳州市| 梅州市| 乌苏市| 乌恰县|