Java中的序列化是一種將物件的狀態轉換為位元組流的機制。反序列化是其逆過程。透過反序列化,可以從位元組流在記憶體中建立實際的 Java 物件。這種機制持續存在於物件中。透過序列化創建的位元組流不依賴任何平台。任何其他平台都可以反序列化在一個平台上序列化的對象,不會有問題。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
因此,序列化和反序列化的整個過程是獨立於JVM的。要序列化類別對象,必須實作 java.io.Serialized 介面。 Java中的Serializing是一個標記介面。它沒有要實現的字段或方法。此過程使類別可序列化,類似於選擇加入過程。
Java中的序列化是由ObjectInputStream和ObjectOutputStream這兩個類別實現的。所需要的只是將它們包裝,以便將它們保存到文件或透過網路發送。
上一節提到的序列化類別ObjectOutputStream包含多種用於寫入各種資料類型的寫入方法,但其中一種方法是最受歡迎的。
public final void writeObject( Object x ) throws IOException
可以使用上面的方法來序列化一個物件。此方法還將其傳送到輸出流。同樣,ObjectInputStream類別包含物件反序列化的方法。
public final Object readObject() throws IOException, ClassNotFoundException
反序列化方法從流中檢索物件並對其進行反序列化。傳回值又是一個對象,因此所需要做的就是將其轉換為相關的資料類型。
成功序列化類別必須滿足兩個條件。
如果有人需要檢查一個類別是否可序列化,簡單的解決方案是檢查該類別是否實作了 java.io.Serializing 方法;如果是,則它是可序列化的。如果不是,那就不是。人們應該注意到,當物件序列化到檔案時,標準做法是為檔案賦予 .ser 副檔名。
如果類別包含這些方法,它們將用於 Java 中的序列化。
Method | Description |
public final void writeObject (Object obj) throws IOException {} | This will write the specified object to the ObjectOutputStream. |
public void flush() throws IOException {} | This will flush the current output stream. |
public void close() throws IOException {} | This will close the current output stream. |
Method | Description |
public final Object readObject() throws IOException, ClassNotFoundException{} | This will read an object from the input stream. |
public void close() throws IOException {} | This will close ObjectInputStream. |
An example in Java is provided here to demonstrate how serialization works in Java. We created an Employee class to study some features, and the code is provided below. This employee class implements the Serializable interface.
public class Employee implements java.io.Serializable { public String name; public String address; public transient int SSN; public int number; public void mailCheck() { System.out.println("Mailing a letter to " + name + " " + address); } }
When this program finishes executing, it will create a file named employee.ser. This program does not provide a guaranteed output, rather it is for explanatory purposes only, and the objective is to understand its use and to work.
import java.io.*; public class SerializeDemo { public static void main(String [] args) { Employee e = new Employee(); e.name = "Rahul Jain"; e.address = "epip, Bangalore"; e.SSN = 114433; e.number = 131; try { FileOutputStream fileOut = new FileOutputStream("https://cdn.educba.com/tmp/employee.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); System.out.printf("Serialized data saved in /tmp/employee.ser"); } catch (IOException i) { i.printStackTrace(); } } }
The below-described DeserializeDemo program deserializes the above Employee object created in the Serialize Demo program.
import java.io.*; public class DeserializeDemo { public static void main(String [] args) { Employee e = null; try { FileInputStream fileIn = new FileInputStream("https://cdn.educba.com/tmp/employee.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Employee) in.readObject(); in.close(); fileIn.close(); } catch (IOException i) { i.printStackTrace(); return; } catch (ClassNotFoundException c) { System.out.println("Employee class is not found"); c.printStackTrace(); return; } System.out.println("Deserialized Employee..."); System.out.println("Name: " + e.name); System.out.println("Address: " + e.address); System.out.println("SSN: " + e.SSN); System.out.println("Number: " + e.number); } }
Output:
Deserialized Employee…
Name: Rahul Jain
Address: epip, Bangalore
SSN: 0
Number:131
Some important points related to the program above are provided below:
Above, we introduced serialization concepts and provided examples. Let’s understand the need for serialization in our concluding remarks.
以上是Java 中的序列化的詳細內容。更多資訊請關注PHP中文網其他相關文章!