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