Serialization
Serialization is the process of storing the state of an object to a sequence of bytes in the secondary storage device. If we want to make a class as Serializable, we should implement the java.io.Serializable interface, It is a marker interface.
By knowing the values associated with the properties, we can determine the state of the object. Hence by serializing a class, all the property values can be saved in the files, database or network. If we don’t want to serialize some of the properties, we can make them as transient.
Transient
Transient is the access modifier in Java, When we declare a variable with a transient keyword, the value the variable can not be saved.
class TransientDemo implements Serializable{
transient int a; // Not Saved
int b; // Saved
}
The process of serialization can be done with the help of ObjectOutputStream. The ObjectOutputStream is a class which writes the primitive data types of Java Objects to an OutputStream. To write the primitive to OutputStream, the class should implement the Serializable interface.
void writeObject(Object obj) is used to write the object to OutputStream.
Serialization Example
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializableDemo implements Serializable {
public String file_path = "/home/chandrashekhar/Desktop/FileOperations.ser";
int i = 10;
transient int j = 20;
String s = "onlinetutorialspoint";
public static void main(String[] args) {
SerializableDemo demo = new SerializableDemo();
demo.serializeToFS(demo, demo.file_path);
}
public void serializeToFS(SerializableDemo demoBean, String path) {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(new File(path)));
oos.writeObject(demoBean);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
oos = null;
}
}
}
After running the above example, the program will create a FileOperations.ser file on your specified path. The sequence of bytes will be stored in the FileOperations.ser.
De serialization
Serialization has also involved the process of getting back the state of an object, after reading the sequence of bytes from the stored file. By using the readObject() method available in the ObjectInputStream class, the data will be deserialized.
DeSerialization Example
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
public class DeSerializableDemo implements Serializable {
public static void main(String[] args) {
String file_path = "/home/chandrashekhar/Desktop/FileOperations.ser";
DeSerializableDemo demo = new DeSerializableDemo();
demo.deserializeFromFS(file_path);
}
public void deserializeFromFS(String path) {
ObjectInputStream ois = null;
SerializableDemo operations = null;
try {
ois = new ObjectInputStream(new FileInputStream(new File(path)));
operations = (SerializableDemo) ois.readObject();
System.out.println("Serialization i: " + operations.i);
System.out.println("Serialization j: " + operations.j);
System.out.println("Serialization Name: " + operations.s);
ois.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
ois = null;
operations = null;
}
}
}
Output:
Serialization i: 10
Serialization j: 0
Serialization Name: onlinetutorialspoint
On the above output, the "j"
value is "0"
. Because "j"
is declared as transient, hence the default value is applied to the primitive.
You may also Like:
How to writing data to a file in Java ?
How to count number of lines characters and words in a file Java ?
How to Search a file in a Directory using Java ?
Happy Learning 🙂