Serialization is a concept that allows us to convert the state of a Java object into a certain format so that this Java object can be stored somewhere and then it will be used by another process.
Normally, when using Serialization, our Java object will be converted to byte streams and we can store this byte stream in memory, on disk, transmitted over the network to a certain server or can also save them in the database.
And when another process uses a serialized Java object, it converts the Serialization format to the state of the original Java object. Thanks to that, the process can reuse our Java object.
In order for an object to be able to use serialization, we must let our object implement an interface called java.io.Serializable. This interface does not have any methods to implement.
To understand better, I will create an example to prove what I just said!
For example, I have an object Student:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.huongdanjava.javaexample; public class Student { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
Because I want to use Serialization, I will implement the Serializable interface for the Student object as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.huongdanjava.javaexample; import java.io.Serializable; public class Student implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
As you can see, I added a variable named serialVersionUID into my Student object. The purpose of this variable is to make sure before and after the conversion, our Student class is the same.
OK, now we are going to write code to convert the Student object and save its byte streams to a certain file!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava.javaexample; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class SerializationExample { public static void main(String[] args) { // Create Student object Student student = new Student(); student.setName("Khanh"); student.setAge(30); // Use FileOutputStream to save the Student object into a file try ( FileOutputStream fos = new FileOutputStream("E:\\student.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); ) { oos.writeObject(student); } catch (IOException i) { i.printStackTrace(); } } } |
Now, a file has been created and if you examine the file that you saved your Student object, you will see something like this:
1 |
’ sr &com.huongdanjava.javaexample.Student I ageL namet Ljava/lang/String;xp t Khanh |
So how can other process use the file we created in the example above, we write the next code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.huongdanjava.javaexample; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class DeserializationExample { public static void main(String[] args) { Student student = null; try ( FileInputStream fos = new FileInputStream("E:\\student.txt"); ObjectInputStream oos = new ObjectInputStream(fos); ) { student = (Student) oos.readObject(); } catch (IOException i) { i.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.err.println(student.getName()); System.err.println(student.getAge()); } } |
Kết quả:
1 2 |
Khanh 30 |
As you saw before and after the conversion, your Student object still has the information that we saved earlier.