一文帶你徹底理解Java序列化和反序列化
Java序列化是什麼?
Java序列化是指把Java物件轉換為位元組序列的過程,Java反序列化是指把位元組序列恢復為Java物件的過程。
反序列化: 客戶端重檔案,或者網路中獲取到檔案以後,在記憶體中重構物件。
序列化: 物件序列化的最重要的作用是傳遞和儲存物件的時候,保證物件的完整性和可傳遞性。方便位元組可以在網路上傳輸以及儲存在本地檔案。
為什麼需要序列化和反序列化
實現分散式
核心在於RMI,可以利用物件序列化執行遠端主機上的服務,實現執行的時候,就像在本地上執行Java物件一樣。
實現遞迴儲存物件
進行序列化的時候,單單並不是儲存一個物件,而是遞迴的儲存一整個物件序列,即遞迴儲存,通過反序列化,可以遞迴的得到一整個物件序列。
序列資訊可以永久儲存
用於序列化的資訊,可以永久儲存為檔案,或者儲存在資料庫中,在使用的時候,再次隨時恢復到記憶體中,實現記憶體中的類的資訊可以永久的儲存。
資料格式統一
比照Linux的一切皆檔案的思想,同時Java也是這樣的思想,讓資料格式儘可能的統一,讓物件,檔案,資料,等等許許多多不同的格式,都讓其統一,以及儲存。實現資料可以完整的傳輸和儲存。然後進行反序列化還原,即,物件還是物件,檔案還是檔案。
實現Java序列化和反序列化
要進行反序列化需要實現一個介面。即 Serializabei介面。
程式碼如下
需要轉化的類
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
繼續實現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,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(); } }
檢視輸出的結構
common.lang.User1@6ef64f64[
name=yaomy
age=23
]
common.lang.User1@184c9860[
name=<null>
age=0
]
根據輸出的結果可以看到,對User1進行序列化然後再反序列化之後物件的屬性都恢復成了預設值,即,之前那個物件的狀態沒有被持久儲存下來,這就是Externalization和Serialization介面的區別,其中前者介面會被恢復成為預設值,後者介面不會恢復預設值。
如果需要恢復,這裡就需要重寫兩個抽象方法,分別是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,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(); } }
輸出的結果
common.lang.User1@6cd66725[
name=yaomy
age=23
]
common.lang.User1@19160e64[
name=yaomy
age=23
]
靜態變數的序列化
例項
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方法中,物件序列化以後,修改靜態變數的數值,再把序列化後的物件讀取出來,此時輸出的值為10.
理解如下: 列印的staticVar是從讀取物件裡獲得的,列印10的原因是因為序列化時,不儲存靜態變數,只儲存記憶體中的狀態。此時修改靜態變數的值,修改的是類中的值,輸出的也是類中的值,和記憶體無關。
Transient關鍵字
Transient關鍵字,加上以後,可以阻止該變數被序列化到檔案中,反序列化以後,變數的值設定為初始值。
以上就是一文帶你徹底理解Java序列化和反序列化的詳細內容,更多關於Java序列化和反序列化的資料請關注我們其它相關文章!