JAVA serialization and deserialization

Keywords: Java network JDK

1. Why do we need serialization and deserialization

We know that when two processes communicate remotely, they can send each other various types of data, including text, pictures, audio, video and so on, which will be transmitted on the network in the form of binary sequences. So when two Java processes communicate, can object transfer be achieved between them? The answer is yes. How to do it? This requires Java serialization and deserialization. In other words, on the one hand, the sender needs to convert the Java object into a sequence of bytes and then transmit it over the network; on the other hand, the receiver needs to recover the Java object from the sequence of bytes.

When we understand why Java serialization and deserialization are needed, we naturally think about the benefits of Java serialization. Its benefits:

Firstly, data persistence is realized. Data can be stored permanently on hard disk (usually in files) by serialization.

The second is to use serialization to realize remote communication, that is, to transmit the byte sequence of objects on the network.

 

2. How to realize Java serialization and deserialization

1) Serialization API in JDK class library

java.io.ObjectOutputStream: Represents the object output stream

Its writeObject(Object obj) method can serialize the obj object specified by the parameter and write the sequence of bytes to a target output stream.

java.io.ObjectInputStream: Represents the object input stream

Its readObject() method reads byte sequences from the source input stream, then deserializes them into an object and returns them.

2) Requirements for serialization

Only objects of classes that implement Serializable or Externalizable interfaces can be serialized, otherwise exceptions are thrown.
 

Serialization:

It is to convert the objects in memory into byte sequences, so as to facilitate the persistence to disk or network transmission.

Object serialization can be divided into two steps:

First: Convert objects to byte arrays
Second: Store byte arrays on disk

package com.jieke.io;
import java.io.Serializable;
 
/**
 *Title:Student category
 *Description:Student Classes for Implementing Serialized Interfaces
 *Copyright: copyright(c) 2012
 *Filename: Student.java
 *@author Wang Luqing
 *@version 1.0
 */
@Data
public class Student implements Serializable
{
 private String name;
 private char sex;
 private int year;
 private double gpa;
 
 public Student()
 {
 
 }
 public Student(String name,char sex,int year,double gpa)
 {
  this.name = name;
  this.sex = sex;
  this.year = year;
  this.gpa = gpa;
 }

}
import java.io.*;
 
/**
 *Title:Applied Students
 *Description:Serialization and Deserialization of Student Class Instances
 *Copyright: copyright(c) 2012
 *Filename: UseStudent.java
 *@author Wang Luqing
 *@version 1.0
 */
 
public class UseStudent
{
 public static void main(String[] args)
 {
  Student st = new Student("Tom",'M',20,3.6);
  File file = new File("O:\\Java\\com\\jieke\\io\\student.txt");
  try
  {
   file.createNewFile();
  }
  catch(IOException e)
  {
   e.printStackTrace();
  }
  try
  {
   //Student object serialization process
   FileOutputStream fos = new FileOutputStream(file);
   ObjectOutputStream oos = new ObjectOutputStream(fos);
   oos.writeObject(st);
   oos.flush();
   oos.close();
   fos.close();
 
   //Student object deserialization process
   FileInputStream fis = new FileInputStream(file);
   ObjectInputStream ois = new ObjectInputStream(fis);
   Student st1 = (Student) ois.readObject();
   System.out.println("name = " + st1.getName());
   System.out.println("sex = " + st1.getSex());
   System.out.println("year = " + st1.getYear());
   System.out.println("gpa = " + st1.getGpa());
   ois.close();
   fis.close();
  }
  catch(ClassNotFoundException e)
  {
   e.printStackTrace();
  }
  catch (IOException e)
  {
   e.printStackTrace();
  }				
 }
}

 

Posted by KCKTechs on Tue, 23 Jul 2019 06:20:01 -0700