Object Serialization and Deserialization in java

Object Serialization and Deserialization in java

Serialization and deserialization in the Java programming language are file management concepts or methods that allow Java users to convert file handling data objects into byte stream serialization methods, and to recreate and deserialize data objects from byte streams. Serialization and deserialization file method processes in Java are commonly used to store file object data information objects into existing files, send them over an online network, or maintain data across multiple individually connected sessions.

Object Serialization and Deserialization in java

So, let’s explore the serialization and deserialization concepts in Java programming.

Serialization Methods in Java.

The serialization concept in Java file handling processes is used to convert file object data into a sequence of bytes, making it easier to store and process the byte data information, such as to a file, a connected database, or to send it across a system network. The byte stream represents the state of the object in the current file, containing its data and metadata. This can later be corrected by the deserialization process if necessary.

How the serialization concept works in Java.

In the Java file handling process, a file data, info, object is converted to a byte stream using the serialize method. This includes detailed information about the class type, data, and references to other class objects.

In the file handling process, the file object’s class implements the Serializable interface. As such, there is no method to implement in serialize. This indicates in the Java language that the current file object can be serialized.

Java Serializable interface concept.

In the Java file handling process, the Serializable interface indicates that a class is serializable. Serializable does not have any user-defined methods. When a Java user implements a class interface, the Java language environment automatically serializes the object to a file. This assumes that all non-transient fields in the class are also serializable.

import java.io.Serializable;

public class Course implements Serializable {

private int c_id;

private String course_name;

// Here we create a course constructor for the course class

public Course(int c_id, String course_name) {

this.c_id = c_id;

this.course_name = course_name;

}

// Here we create a getters and setters method for the course class

public int getId() {

return c_id;

}

public String getName() {

return course_name;

}

}

Example of serialization in the Java language.

To serialize a file object in Java file handling, Java users typically invoke the ObjectOutputStream class. It is used to create an output stream for a file object, such as data and information stored in a file.

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

public class SerializationIllustration {

public static void main(String[] args) {

Course course = new Course(101, “Java Language”);

try (ObjectOutputStream objotstrm = new ObjectOutputStream(new FileOutputStream(“course.ser”))) {

objotstrm.writeObject(course); // Here we serialize the course content object

System.out.println(“here the user-defined object is serialized -“);

} catch (IOException e) {

e.printStackTrace();

}

}

}

Here in the ObjectOutputStream example above.

  • In this program, ObjectOutputStream.writeObject() serializes the course class object and creates it in a file (course.ser).
  • This file will store the byte representation of the object in the system.

Deserialization concept in Java.

In Java file handling, deserialization is a process or method of converting a byte stream into a copy of the original object. This feature in file handling reads the serialized file data object from an existing file or stream and recreates it as a live object in system memory.

Deserialization example in Java.

To deserialize a file object in the Java file handling process, Java users invoke the ObjectInputStream class, which reads the byte stream from the existing file and recreates the original data info object in the file.

import java.io.FileInputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

public class DeserializationIllustration {

public static void main(String[] args) {

try (ObjectInputStream objtinptstrm = new ObjectInputStream(new FileInputStream(“course.ser”))) {

Course course = (Course) objtinptstrm.readObject(); // Deserialize the course class object here.

System.out.println(“This is where the deserialization process begins -“);

System.out.println(“Course c_id – ” + course.getc_id());

System.out.println(“Course course_name – ” + course.getcourse_name());

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

}

}

}

Here in this example.

  • In the deserialize file handling process, ObjectInputStream.readObject() reads byte stream data from the file (course.ser), recreates the Course class object, and casts it back to its original data type.
  • The deserialize process reverts the file data object to its original state and accesses the Course class fields (such as c_id and course_name) as if they were never serialized to the current file.

Leave a Reply