日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

java的Serialization 机制

發(fā)布時(shí)間:2025/7/25 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java的Serialization 机制 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

基本使用方法???????
??????? Serialization是指把類或者基本的數(shù)據(jù)類型持久化(persistence)到數(shù)據(jù)流(Stream)中,包括文件、字節(jié)流、網(wǎng)絡(luò)數(shù)據(jù)流。?
???????? JAVA中實(shí)現(xiàn)serialization主要靠?jī)蓚€(gè)類:ObjectOuputStream和ObjectInputStream。他們是JAVA IO系統(tǒng)里的OutputStream和InputStream的子類。既然他們是JAVA IO中的流,那么就可以像操作一般的流一樣來(lái)操作他們。下面是他們使用方法:?

Java代碼??
  • 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è)類都是decorator模式的,在創(chuàng)建他們的時(shí)候,都要傳入一個(gè)基于字節(jié)的流,真正在底下存貯序列化數(shù)據(jù)的都是這些流。?
    2. 被持久化的類要實(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ī)器上的類應(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)我的另外一篇博客:http://zzy1943.iteye.com/blog/634553?

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

    javadoc中對(duì)這兩個(gè)類的描述中對(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ì)象所屬的類?
    2、類的簽名?
    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:? Java代碼??
  • private?void?readObject(java.io.ObjectInputStream?stream)??
  • ????throws?IOException,?ClassNotFoundException;??
  • private?void?writeObject(java.io.ObjectOutputStream?stream)??
  • ????throws?IOException??
  • private?void?readObjectNoData()???
  • ????throws?ObjectStreamException;??
  • 轉(zhuǎn)載于:https://www.cnblogs.com/rosepotato/p/3404855.html

    《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

    總結(jié)

    以上是生活随笔為你收集整理的java的Serialization 机制的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。