What is java serialization and deserialization?
The principle of serialization is to convert an object into a stream of bytes so that it can be written to a file, transmitted over a network, or stored in a database. Deserialization is the opposite.
In short, serialization is to convert an object into a byte stream, and deserialization is to restore the byte stream back to an object.
A class is only eligible for serialization if it implements the java.io.Serializable interface.
Define the Person class:
The nationality is declared as transient in this class, so this parameter will not participate in the serialization operation.
1 package com.led.javabase; 2 3 import java.io.Serializable; 4 5 /** 6 * person entity 7 */ 8 public class Person implements Serializable{ 9 private String name; 10 private int age; 11 private String gender; 12 private transient String nationality;//this field will not take part in serialization 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public int getAge() { 23 return age; 24 } 25 26 public void setAge(int age) { 27 this.age = age; 28 } 29 30 public String getGender() { 31 return gender; 32 } 33 34 public void setGender(String gender) { 35 this.gender = gender; 36 } 37 38 public String getNationality() { 39 return nationality; 40 } 41 42 public void setNationality(String nationality) { 43 this.nationality = nationality; 44 } 45 46 public Person(String name, int age, String gender, String nationality) { 47 this.name = name; 48 this.age = age; 49 this.gender = gender; 50 this.nationality = nationality; 51 } 52 }
Serialized instance:
The following example serializes a person instance into the Person.ser file and operates through FileOutputStream and ObjectOutputStream :
1 package com.led.javabase; 2 3 import java.io.*; 4 5 /** 6 * java serialization test 7 * @author Alan 8 */ 9 public class SerializationTest1 { 10 public static void main(String[] args) { 11 Person person = new Person("Jack",11,"male","US"); 12 try { 13 FileOutputStream fos = new FileOutputStream("Person.ser"); 14 ObjectOutputStream oos = new ObjectOutputStream(fos); 15 oos.writeObject(person); 16 oos.close(); 17 fos.close(); 18 System.out.println("Serialization done!"); 19 } catch (FileNotFoundException e) { 20 e.printStackTrace(); 21 } catch (IOException e2) { 22 e2.printStackTrace(); 23 } 24 25 } 26 }
控制台输出:
反序列化实例:
下面的例子通过使用FileInputStream和ObjectInputStream来进行反序列化,通过读取Person.ser文件,将字节流转成Person类的对象,
由于Person类的nationality参数声明为了transient,所以该参数不参与序列化化操作,导致反序列化该参数输出为null。
1 package com.led; 2 3 import com.led.javabase.Person; 4 5 import java.io.*; 6 7 /** 8 * @author Alan 9 * @Description: De-serialization Object 10 * @date 2018/4/22 12:36 11 */ 12 public class DeSerializtion { 13 public static void main(String[] args) { 14 Person o = null; 15 try { 16 FileInputStream fos = new FileInputStream("Person.ser"); 17 ObjectInputStream ois = new ObjectInputStream(fos); 18 o = (Person) ois.readObject(); 19 ois.close(); 20 fos.close(); 21 } catch (FileNotFoundException e) { 22 e.printStackTrace(); 23 return; 24 } catch (IOException e2){ 25 e2.printStackTrace(); 26 return; 27 } catch (ClassNotFoundException e) { 28 e.printStackTrace(); 29 return; 30 } 31 System.out.println("student's name: " + o.getName()); 32 System.out.println("student's age: " + o.getAge()); 33 System.out.println("student's gender: " + o.getGender()); 34 System.out.println("student's nationality: " + o.getNationality()); 35 36 } 37 }
Console output: