Serialization and Deserialization in Java
Serialization in Java is the process of converting an object into a byte stream so that it can be saved to a file or transmitted over a network. Deserialization is the reverse process where the byte stream is converted back into a Java object. This tutorial explains how to serialize and deserialize objects with step-by-step examples.
Step 1: Creating a Serializable Class
To make a class serializable, it must implement the java.io.Serializable
interface. Below is an example:
import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person{name='" + name + "', age=" + age + "}"; } }
Step 2: Serializing an Object
Use the ObjectOutputStream
class to serialize an object to a file. Below is an example:
import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.IOException; public class SerializeExample { public static void main(String[] args) { Person person = new Person("John Doe", 30); try { FileOutputStream fileOut = new FileOutputStream("person.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(person); out.close(); fileOut.close(); System.out.println("Object serialized and saved to person.ser"); } catch (IOException e) { System.out.println("An error occurred: " + e.getMessage()); } } }
Step 3: Deserializing an Object
Use the ObjectInputStream
class to deserialize an object from a file. Below is an example:
import java.io.FileInputStream; import java.io.ObjectInputStream; import java.io.IOException; public class DeserializeExample { public static void main(String[] args) { try { FileInputStream fileIn = new FileInputStream("person.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); Person person = (Person) in.readObject(); in.close(); fileIn.close(); System.out.println("Object deserialized: " + person); } catch (IOException | ClassNotFoundException e) { System.out.println("An error occurred: " + e.getMessage()); } } }
Step 4: Customizing Serialization
You can customize the serialization process by defining the writeObject
and readObject
methods in your class. Below is an example:
import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeUTF(name.toUpperCase()); // Example: Convert name to uppercase } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); this.name = in.readUTF(); } @Override public String toString() { return "Person{name='" + name + "', age=" + age + "}"; } }
Summary
In this tutorial, you learned:
- How to create a serializable class
- How to serialize an object to a file
- How to deserialize an object from a file
- How to customize the serialization process
Serialization is a powerful mechanism for persisting and transferring objects in Java.