java 對象系列化 (Serializable)

 java 對象系列化 (Serializable)

 

對象序列化:就是將一個對象轉換爲二進制的數據流。

如果一個類的對象要想實現對象系列化,則對象所在的類必須實現Serializable接口。在此接口中沒有任何的方法,此接口只是作爲一個標識,表示本來的對象具備了序列化的能力而已。

如果想要完成對象的序列化,則還要依靠ObjectOutputStream類和ObjectInputStream類,前者屬於序列化操作,而後者屬於反序列化操作。

實例: 

  1. import java.io.Serializable; 
  2. public class Person implements Serializable { 
  3.     private String name; 
  4.     private int age; 
  5.  
  6.     public Person(String name, int age) { 
  7.         super(); 
  8.         this.name = name; 
  9.         this.age = age; 
  10.     } 
  11.     public String toString() { 
  12.         return "姓名:" + this.name + "  年齡: " + this.age; 
  13.     } 

使用ObjectOutputStream完成序列化的操作

  1. import java.io.File; 
  2. import java.io.FileOutputStream; 
  3. import java.io.ObjectOutputStream; 
  4. public class ObjectOutputStreamDemo { 
  5.     public static void main(String[] args) throws Exception { 
  6.         File file = new File("D:" + File.separator + "Person.txt"); 
  7.         ObjectOutputStream objectOutputStream = null
  8.         objectOutputStream = new ObjectOutputStream(new FileOutputStream(file)); 
  9.         objectOutputStream.writeObject(new Person("singsong"23)); 
  10.         objectOutputStream.close(); 
  11.     } 

使用ObjectInputStream完成反序列化操作 

  1. import java.io.File; 
  2. import java.io.FileInputStream; 
  3. import java.io.ObjectInputStream; 
  4. public class ObjectInputStreamDemo { 
  5.     public static void main(String[] args) throws Exception { 
  6.         File file = new File("D:" + File.separator + "person.txt"); 
  7.         ObjectInputStream objectInputStream = null
  8.         objectInputStream = new ObjectInputStream(new FileInputStream(file)); 
  9.         Object object = objectInputStream.readObject(); 
  10.         Person person = (Person) object; 
  11.         System.out.println(person); 
  12.     } 

運行結果:

  1. 姓名:singsong  年齡: 23 

以上的操作實際上是將整個的對象進行了序列化的操作,如果假如類中某個屬性不希望被序列化的話,則要使用transient關鍵字聲明。

實例:修改Person類:

  1. import java.io.Serializable; 
  2. public class Person implements Serializable { 
  3.     private transient String name; 
  4.     private transient int age; 
  5.     public Person(String name, int age) { 
  6.         super(); 
  7.         this.name = name; 
  8.         this.age = age; 
  9.     } 
  10.     public String toString() { 
  11.         return "姓名:" + this.name + "  年齡: " + this.age; 
  12.     } 

然後再進行序列化和反系列化操作,運行結果:

  1. 姓名:null  年齡: 0 

顯示的是默認值

既然可以對一個對象進行系列化,那麼能不能同時對多個對象一起進行系列化操作

Object類可以接收任意的引用數據類型,包括數組。

  1. import java.io.File; 
  2. import java.io.FileInputStream; 
  3. import java.io.FileOutputStream; 
  4. import java.io.ObjectInputStream; 
  5. import java.io.ObjectOutputStream; 
  6. public class ArraySerDemo { 
  7.     public static void main(String[] args) throws Exception { 
  8.         Person person[] = { new Person("張三"21), new Person("李四"22), 
  9.                 new Person("王五"23) }; 
  10.         ser(person); 
  11.         Person[] persons = (Person[]) dser(); 
  12.         print(person); 
  13.     } 
  14.     public static void ser(Object object) throws Exception { 
  15.         File file = new File("D:" + File.separator + "Person.txt"); 
  16.         ObjectOutputStream objectOutputStream = null
  17.         objectOutputStream = new ObjectOutputStream(new FileOutputStream(file)); 
  18.         objectOutputStream.writeObject(object); 
  19.         objectOutputStream.close(); 
  20.     } 
  21.     public static Object dser() throws Exception { 
  22.         Object temp = null
  23.         File file = new File("D:" + File.separator + "person.txt"); 
  24.         ObjectInputStream objectInputStream = null
  25.         objectInputStream = new ObjectInputStream(new FileInputStream(file)); 
  26.  
  27.         temp = objectInputStream.readObject(); 
  28.         return temp; 
  29.     } 
  30.     public static void print(Person persons[]) { 
  31.         for (Person person : persons) 
  32.             System.out.println(person); 
  33.     } 

運行結果:

  1. 姓名:張三  年齡: 21 
  2. 姓名:李四  年齡: 22 
  3. 姓名:王五  年齡: 23 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章