日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java的Serialization 机制

發布時間:2025/7/25 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java的Serialization 机制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

基本使用方法???????
??????? Serialization是指把類或者基本的數據類型持久化(persistence)到數據流(Stream)中,包括文件、字節流、網絡數據流。?
???????? JAVA中實現serialization主要靠兩個類:ObjectOuputStream和ObjectInputStream。他們是JAVA IO系統里的OutputStream和InputStream的子類。既然他們是JAVA IO中的流,那么就可以像操作一般的流一樣來操作他們。下面是他們使用方法:?

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. 這兩個類都是decorator模式的,在創建他們的時候,都要傳入一個基于字節的流,真正在底下存貯序列化數據的都是這些流。?
    2. 被持久化的類要實現Serializable接口,這個接口沒有任何函數,只是一個標記接口。如果在一臺機器上進行序列化,把得到的數據傳送到另外一個機器上進行反序列化,那么這兩臺機器上的類應該是完全一樣的,否則序列化是不會成功的。?
    3. 切記不要把上面代碼中的bos用toString得到String,然后再從這個String中得到ByteArrayInputStream,再進行反序列化。bos是以字節存貯的,轉成以字符存貯的String必然會造成數據的變化,而從String中到的byte[]也不會是之前那個byte[]了。我遇到過這個問題,是因為我想把序列化之后的數據存在xml文件中。這個問題的具體解決方法見我的另外一篇博客:http://zzy1943.iteye.com/blog/634553?

    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.?


    默認的序列化機制寫到流中的數據有:?
    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:? 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;??
  • 轉載于:https://www.cnblogs.com/rosepotato/p/3404855.html

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

    總結

    以上是生活随笔為你收集整理的java的Serialization 机制的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。