Introduction
Serialization is a way to you save the specific state of the program in a file so that you can retrieve that file in the class at some other point. A class implements the Serializable interface to perform serialization.
Creating the serialized file
Use the following procedure to create the serialized file:
- Create a FileOutputStream object. A FileOutputStream object knows how to connect to a file that is already created or it creates the file and then connects with it. It is a connection stream that writes bytes to the file.
FileOutputStream fos=new FileOutputStream("MyFile.ser");
- Now create an ObjectOutputStream Object. An ObjectOutputStream object allows you to write the Object, but it cannot directly write to the file. So we use a FileOutputStream object to write the object to the file. ObjectOutputStream is a chain stream that converts the object into data that can be written into streams.
ObjectOutputStream oos=new ObjectOutputStream(fos);
- Now we write the object in the file using the writeObject() method of the ObjectOutputStream object.
oos.writeObject(object reference);
- Now close the objectoutputstream object using the close() method. Closing the stream at the top will close the one underneath it, so FileOutputStream automatically closes.
oos.close();
A diagrammatic representation of the preceding process is:
An example of how to create a serialized file is:
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- public class SerializableExample implements Serializable {
- int x;
- int y;
-
-
-
- public static void main(String[] args) {
-
- SerializableExample se=new SerializableExample();
- se.x=3;
- se.y=4;
- try {
- FileOutputStream fos=new FileOutputStream("File.ser");
- ObjectOutputStream oos=new ObjectOutputStream(fos);
- oos.writeObject(se);
- oos.close();
- System.out.println("File is created");
- }catch (FileNotFoundException e) {
-
- e.printStackTrace();
- }catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }
A file with the name File.ser will be created in the folder in which you have created your class. This file.ser can be used in another part of the class and you can get the object and its state by deserializing the file.
Deserializing a file
Use the following procedure to deserialize a file:
- Create a FileInputStream Object. A FileInputStream Object creates a connection with the file that is to be deserialized. If the file does not exist then it gives the exception FileNotFound.
FileInputStream fis=new FileInputStream("File.ser");
- Create a ObjectInputStream object. A ObjectInputStream object transfers the bytes from the file into objects.
ObjectInputStream ois=new ObjectInputStream(fis);
- Now we use the ObjectInputStream readObject() method to get the objects. Since the method returns the object as an Object type, we downcast it to our class name.
Classname reference=(Classname)ois.readObject();
- Now close the ObjectInputStream using the close() method. Since the ObjectInputStream is a high level stream, closing it means all the streams, like FileInputStream, are automatically closed.
ois.close();
A diagrammatic representation of the above process is:
The code to deserialize the file that we serialized above is:
- try {
- FileInputStream fis = new FileInputStream("File.ser");
- ObjectInputStream ois=new ObjectInputStream(fis);
- SerializableExample se1=(SerializableExample)ois.readObject();
- System.out.println(se1.x);
- System.out.println(se1.y);
- }catch (FileNotFoundException e) {
-
- e.printStackTrace();
- }catch (IOException e) {
-
- e.printStackTrace();
- }
The output will be the value of the instance variable of the object that was serialized. So, the output will be 3 and 4.
I hope this article was useful to you and thanks for reading it.