序列化和反序列化

1.概念

java中的對(duì)象是存放在內(nèi)存中的谱煤,當(dāng)程序運(yùn)行結(jié)束,內(nèi)存中的對(duì)象也會(huì)隨之被系統(tǒng)回收掉邓了。要想保存對(duì)象就得序列化。序列化后的對(duì)象媳瞪,可以保存在文件或上傳到服務(wù)器骗炉。從文件或網(wǎng)絡(luò)數(shù)據(jù)流中取出對(duì)象,就是反序列化蛇受。

2.Serializable

java.io.Serializable是一個(gè)空接口句葵,用來(lái)標(biāo)志對(duì)象可以被序列化。這個(gè)標(biāo)志可以被java.io.ObjectOutputStreamjava.io.ObjectInputStream識(shí)別,將對(duì)象序列化為二進(jìn)制字節(jié)流或從二進(jìn)制字節(jié)流反序列化為對(duì)象實(shí)例乍丈。

3.注意事項(xiàng)

  • 若類的成員變量不需要序列化剂碴,可以用transient修飾,如:
    private transient String name;
  • 靜態(tài)變量和transient修飾的變量不能被序列化
  • 反序列化創(chuàng)建的對(duì)象轻专,與原對(duì)象數(shù)據(jù)基本相同忆矛,但內(nèi)存地址不相同,不是同一個(gè)對(duì)象请垛。需要注意單例對(duì)象的反序列化催训,可能會(huì)在內(nèi)存中產(chǎn)生多個(gè)單例對(duì)象∽谑眨可以使用readResolve()方法解決漫拭,這個(gè)方法控制最終反序列化生成的對(duì)象。readResolve()writeReplace()不是ObjectSerializable的方法混稽,是java默認(rèn)規(guī)范的方法
* <p>Serializable classes that need to designate an alternative object to be
 * used when writing an object to the stream should implement this
 * special method with the exact signature:
 *
 * <PRE>
 * ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
 * </PRE><p>
 *
 * This writeReplace method is invoked by serialization if the method
 * exists and it would be accessible from a method defined within the
 * class of the object being serialized. Thus, the method can have private,
 * protected and package-private access. Subclass access to this method
 * follows java accessibility rules. <p>
 *
 * Classes that need to designate a replacement when an instance of it
 * is read from the stream should implement this special method with the
 * exact signature.
 *
 * <PRE>
 * ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
 * </PRE><p>
  • 如果想對(duì)某些成員變量做特殊處理采驻,比如取值范圍校驗(yàn)、加密數(shù)據(jù)荚坞,可以通過(guò)writeObject()readObject()進(jìn)行挑宠。同樣的這兩個(gè)方法也不是ObjectSerializable的方法
 * Classes that require special handling during the serialization and
 * deserialization process must implement special methods with these exact
 * signatures:
 *
 * <PRE>
 * private void writeObject(java.io.ObjectOutputStream out)
 *     throws IOException
 * private void readObject(java.io.ObjectInputStream in)
 *     throws IOException, ClassNotFoundException;
 * private void readObjectNoData()
 *     throws ObjectStreamException;
 * </PRE>
 *
 * <p>The writeObject method is responsible for writing the state of the
 * object for its particular class so that the corresponding
 * readObject method can restore it.  The default mechanism for saving
 * the Object's fields can be invoked by calling
 * out.defaultWriteObject. The method does not need to concern
 * itself with the state belonging to its superclasses or subclasses.
 * State is saved by writing the individual fields to the
 * ObjectOutputStream using the writeObject method or by using the
 * methods for primitive data types supported by DataOutput.
 *
 * <p>The readObject method is responsible for reading from the stream and
 * restoring the classes fields. It may call in.defaultReadObject to invoke
 * the default mechanism for restoring the object's non-static and
 * non-transient fields.  The defaultReadObject method uses information in
 * the stream to assign the fields of the object saved in the stream with the
 * correspondingly named fields in the current object.  This handles the case
 * when the class has evolved to add new fields. The method does not need to
 * concern itself with the state belonging to its superclasses or subclasses.
 * State is saved by writing the individual fields to the
 * ObjectOutputStream using the writeObject method or by using the
 * methods for primitive data types supported by DataOutput.
 *
 * <p>The readObjectNoData method is responsible for initializing the state of
 * the object for its particular class in the event that the serialization
 * stream does not list the given class as a superclass of the object being
 * deserialized.  This may occur in cases where the receiving party uses a
 * different version of the deserialized instance's class than the sending
 * party, and the receiver's version extends classes that are not extended by
 * the sender's version.  This may also occur if the serialization stream has
 * been tampered; hence, readObjectNoData is useful for initializing
 * deserialized objects properly despite a "hostile" or incomplete source
 * stream.
  • private static final long serialVersionUID = 5316419709815709313L;版本id可以直接指定,如果不設(shè)置颓影,會(huì)根據(jù)類的信息動(dòng)態(tài)生成。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末懒鉴,一起剝皮案震驚了整個(gè)濱河市诡挂,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌临谱,老刑警劉巖璃俗,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異悉默,居然都是意外死亡城豁,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)抄课,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)唱星,“玉大人,你說(shuō)我怎么就攤上這事跟磨〖淞模” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵抵拘,是天一觀的道長(zhǎng)哎榴。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么尚蝌? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任迎变,我火速辦了婚禮,結(jié)果婚禮上飘言,老公的妹妹穿的比我還像新娘衣形。我一直安慰自己,他們只是感情好热凹,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布泵喘。 她就那樣靜靜地躺著,像睡著了一般般妙。 火紅的嫁衣襯著肌膚如雪纪铺。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,246評(píng)論 1 308
  • 那天碟渺,我揣著相機(jī)與錄音鲜锚,去河邊找鬼。 笑死苫拍,一個(gè)胖子當(dāng)著我的面吹牛芜繁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播绒极,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼骏令,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了垄提?” 一聲冷哼從身側(cè)響起榔袋,我...
    開(kāi)封第一講書(shū)人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎铡俐,沒(méi)想到半個(gè)月后凰兑,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡审丘,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年吏够,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片滩报。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡锅知,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出露泊,到底是詐尸還是另有隱情喉镰,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布惭笑,位于F島的核電站侣姆,受9級(jí)特大地震影響生真,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜捺宗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一柱蟀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蚜厉,春花似錦长已、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至贰健,卻和暖如春勿锅,著一層夾襖步出監(jiān)牢的瞬間勇哗,已是汗流浹背舟铜。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工菊霜, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人脊另。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓导狡,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親偎痛。 傳聞我的和親對(duì)象是個(gè)殘疾皇子旱捧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359