<form id="dlljd"></form>
        <address id="dlljd"><address id="dlljd"><listing id="dlljd"></listing></address></address>

        <em id="dlljd"><form id="dlljd"></form></em>

          <address id="dlljd"></address>
            <noframes id="dlljd">

              聯系我們 - 廣告服務 - 聯系電話:
              您的當前位置: > 關注 > > 正文

              世界速看:java的序列化機制是什么?java序列化ID的作用

              來源:CSDN 時間:2023-03-07 11:31:48

              序列化ID的作用:

              序列化ID決定著是否能夠成功反序列化!簡單來說,java的序列化機制是通過在運行時判斷類的serialVersionUID來驗證版本一致性的。在進行反序列化時,JVM會把傳來的字節流中的serialVersionUID與本地實體類中的serialVersionUID進行比較,如果相同則認為是一致的,便可以進行反序列化,否則就會報序列化版本不一致的異常。

              通俗意思就是:


              (相關資料圖)

              不加序列化ID:

              某個類進行序列化保存本地或者進行網絡傳輸反序列化時,如果修改了該類,則序列化ID不一致,報錯!

              加序列化:

              某個類進行序列化保存本地或者進行網絡傳輸反序列化時,如果修改了該類,則原本類中存在的值,可取出,不存在的,取默認,不會報錯!

              看例子:

              1、不加序列化ID執行序列化:

              import java.io.*;public class Solution implements Serializable {    String name = "abc";    public static void main(String[] args) throws IOException, ClassNotFoundException {        Solution s = new Solution();        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("a.txt"));        objectOutputStream.writeObject(s);        objectOutputStream.close();    }}

              2、修改類,在類屬性中添加age屬性,進行反序列化:

              import java.io.*;public class Solution implements Serializable {    String name = "abc";    int age = 10;    public static void main(String[] args) throws IOException, ClassNotFoundException {        FileInputStream fis = new FileInputStream("a.txt");        ObjectInputStream inputStream = new ObjectInputStream(fis);        Solution s1 = (Solution) inputStream.readObject();        System.out.println(s1.name);        System.out.println(s1.age);    }}

              3、結果:

              可以發現,修改了類之后,serialVersionUID發生了改變,反序列化失敗,連name屬性都提取不出!

              1、加序列化ID進行序列化:

              import java.io.*;public class Solution implements Serializable {    private final static long serialVersionUID = 20000L;    String name = "abc";    public static void main(String[] args) throws IOException, ClassNotFoundException {        Solution s = new Solution();        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("a.txt"));        objectOutputStream.writeObject(s);        objectOutputStream.close();    }}

              2、在類中添加age屬性,進行反序列化:

              import java.io.*;public class Solution implements Serializable {    private final static long serialVersionUID = 20000L;    String name = "abc";     int age = 10;    public static void main(String[] args) throws IOException, ClassNotFoundException {        FileInputStream fis = new FileInputStream("a.txt");        ObjectInputStream inputStream = new ObjectInputStream(fis);        Solution s1 = (Solution) inputStream.readObject();        System.out.println(s1.name);        System.out.println(s1.age);    }}

              3、結果:

              可以看到,name值取出來了,而且age值為默認0!

              因為age沒有進行序列化,所以保存的字節流文件中并沒有age對應值,我們只能取出Solution類中的name屬性的值,因為序列化的字節流中存在,當序列化ID相同時,反序列化執行成功!

              責任編輯:

              標簽:

              相關推薦:

              精彩放送:

              新聞聚焦
              Top 中文字幕在线观看亚洲日韩