一文帶你徹底理解Java序列化和反序列化
Java序列化是什么?
Java序列化是指把Java對象轉(zhuǎn)換為字節(jié)序列的過程,Java反序列化是指把字節(jié)序列恢復為Java對象的過程。
反序列化: 客戶端重文件,或者網(wǎng)絡(luò)中獲取到文件以后,在內(nèi)存中重構(gòu)對象。
序列化: 對象序列化的最重要的作用是傳遞和保存對象的時候,保證對象的完整性和可傳遞性。方便字節(jié)可以在網(wǎng)絡(luò)上傳輸以及保存在本地文件。
為什么需要序列化和反序列化
實現(xiàn)分布式
核心在于RMI,可以利用對象序列化運行遠程主機上的服務(wù),實現(xiàn)運行的時候,就像在本地上運行Java對象一樣。
實現(xiàn)遞歸保存對象
進行序列化的時候,單單并不是保存一個對象,而是遞歸的保存一整個對象序列,即遞歸保存,通過反序列化,可以遞歸的得到一整個對象序列。
序列信息可以永久保存
用于序列化的信息,可以永久保存為文件,或者保存在數(shù)據(jù)庫中,在使用的時候,再次隨時恢復到內(nèi)存中,實現(xiàn)內(nèi)存中的類的信息可以永久的保存。
數(shù)據(jù)格式統(tǒng)一
比照Linux的一切皆文件的思想,同時Java也是這樣的思想,讓數(shù)據(jù)格式盡可能的統(tǒng)一,讓對象,文件,數(shù)據(jù),等等許許多多不同的格式,都讓其統(tǒng)一,以及保存。實現(xiàn)數(shù)據(jù)可以完整的傳輸和保存。然后進行反序列化還原,即,對象還是對象,文件還是文件。
實現(xiàn)Java序列化和反序列化
要進行反序列化需要實現(xiàn)一個接口。即 Serializabei接口。
代碼如下
需要轉(zhuǎn)化的類
package common.lang;import java.io.Serializable;import org.apache.commons.lang3.builder.ToStringBuilder;import org.apache.commons.lang3.builder.ToStringStyle;public class User1 implements Serializable{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append('name', name) .append('age', age) .toString(); }}
進行序列化,以及反序列化
package common.lang;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class SerializableDemo1 { public static void main(String[] args) throws Exception, IOException { //初始化對象 User1 user = new User1(); user.setName('yaomy'); user.setAge(23); System.out.println(user); //序列化對象到文件中 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream('template')); oos.writeObject(user); oos.close(); //反序列化 File file = new File('template'); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); User1 newUser = (User1)ois.readObject(); System.out.println(newUser.toString()); }}
另一個序列化接口 Externalizable
繼續(xù)實現(xiàn)Externalizable接口
package common.lang;import java.io.Externalizable;import java.io.IOException;import java.io.ObjectInput;import java.io.ObjectOutput;import org.apache.commons.lang3.builder.ToStringBuilder;import org.apache.commons.lang3.builder.ToStringStyle;public class User1 implements Externalizable{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append('name', name) .append('age', age) .toString(); } @Override public void writeExternal(ObjectOutput out) throws IOException { // TODO Auto-generated method stub } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { // TODO Auto-generated method stub }}
進行序列化以及反序列化
package common.lang;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class SerializableDemo1 { public static void main(String[] args) throws Exception, IOException { //初始化對象 User1 user = new User1(); user.setName('yaomy'); user.setAge(23); System.out.println(user); //序列化對象到文件中 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream('template')); oos.writeObject(user); oos.close(); //反序列化 File file = new File('template'); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); User1 newUser = (User1)ois.readObject(); System.out.println(newUser.toString()); ois.close(); }}
查看輸出的結(jié)構(gòu)
common.lang.User1@6ef64f64[name=yaomyage=23]common.lang.User1@184c9860[name=<null>age=0]
根據(jù)輸出的結(jié)果可以看到,對User1進行序列化然后再反序列化之后對象的屬性都恢復成了默認值,即,之前那個對象的狀態(tài)沒有被持久保存下來,這就是Externalization和Serialization接口的區(qū)別,其中前者接口會被恢復成為默認值,后者接口不會恢復默認值。
如果需要恢復,這里就需要重寫兩個抽象方法,分別是writeExternal與readExternal兩個抽象方法。
package common.lang;import java.io.Externalizable;import java.io.IOException;import java.io.ObjectInput;import java.io.ObjectOutput;import org.apache.commons.lang3.builder.ToStringBuilder;import org.apache.commons.lang3.builder.ToStringStyle;public class User1 implements Externalizable{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append('name', name) .append('age', age) .toString(); } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(name); out.writeInt(age); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { name = (String)in.readObject(); age = in.readInt(); }}
package common.lang;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class SerializableDemo1 { public static void main(String[] args) throws Exception, IOException { //初始化對象 User1 user = new User1(); user.setName('yaomy'); user.setAge(23); System.out.println(user); //序列化對象到文件中 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream('template')); oos.writeObject(user); oos.close(); //反序列化 File file = new File('template'); ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); User1 newUser = (User1)ois.readObject(); System.out.println(newUser.toString()); ois.close(); }}
輸出的結(jié)果
common.lang.User1@6cd66725[name=yaomyage=23]common.lang.User1@19160e64[name=yaomyage=23]
靜態(tài)變量的序列化
實例
public class Test implements Serializable { private static final long serialVersionUID = 1L; public static int staticVar = 5; public static void main(String[] args) { try { //初始時staticVar為5 ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream('result.obj')); out.writeObject(new Test()); out.close(); //序列化后修改為10 Test.staticVar = 10; ObjectInputStream oin = new ObjectInputStream(new FileInputStream( 'result.obj')); Test t = (Test) oin.readObject(); oin.close(); //再讀取,通過t.staticVar打印新的值 System.out.println(t.staticVar); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }}
代碼闡述一下過程,在main方法中,對象序列化以后,修改靜態(tài)變量的數(shù)值,再把序列化后的對象讀取出來,此時輸出的值為10.
理解如下: 打印的staticVar是從讀取對象里獲得的,打印10的原因是因為序列化時,不保存靜態(tài)變量,只保存內(nèi)存中的狀態(tài)。此時修改靜態(tài)變量的值,修改的是類中的值,輸出的也是類中的值,和內(nèi)存無關(guān)。
Transient關(guān)鍵字
Transient關(guān)鍵字,加上以后,可以阻止該變量被序列化到文件中,反序列化以后,變量的值設(shè)定為初始值。
以上就是一文帶你徹底理解Java序列化和反序列化的詳細內(nèi)容,更多關(guān)于Java序列化和反序列化的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享2. jsp文件下載功能實現(xiàn)代碼3. asp.net core項目授權(quán)流程詳解4. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法5. CSS3實現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效6. XMLHTTP資料7. ASP常用日期格式化函數(shù) FormatDate()8. html中的form不提交(排除)某些input 原創(chuàng)9. CSS3中Transition屬性詳解以及示例分享10. ASP基礎(chǔ)入門第八篇(ASP內(nèi)建對象Application和Session)
