In Java, the classes ObjectInputStream and ObjectOutputStream are used for object serialization and deserialization. Serialization refers to the process of converting an object into a byte stream so that it can be saved to a file, transmitted over a network, or stored in memory for later use. Deserialization is the reverse process, where a byte stream is converted back into an object.
The ObjectOutputStream class is used to write (serialize) Java objects to an output stream, typically to a file. It allows you to save objects in a form that can be restored later.
Basic Syntax for ObjectOutputStream:
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.ser"));
Key Methods:
writeObject(Object obj): Serializes the specified object and writes it to the underlying output stream.// Example: Writing an Object to a Fileimport java.io.FileOutputStream;import java.io.ObjectOutputStream;import java.io.Serializable;class Student implements Serializable { private static final long serialVersionUID = 1L; String name; int age; public Student(String name, int age) { this.name = name; this.age = age; }}public class SerializeExample { public static void main(String[] args) { try { // Create an instance of Student Student student = new Student("John Doe", 20); // Create an ObjectOutputStream FileOutputStream fileOut = new FileOutputStream("student.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); // Serialize the object out.writeObject(student); // Close the stream out.close(); fileOut.close(); System.out.println("Serialized data is saved in student.ser"); } catch (Exception e) { e.printStackTrace(); } }}Student class and serialize it using ObjectOutputStream.student.ser.The ObjectInputStream class is used to read (deserialize) objects from an input stream, such as a file or a network connection. It reconstructs an object from its byte stream form.
Basic Syntax for ObjectInputStream:
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.ser"));
Key Methods:
Object readObject(): Reads an object from the input stream and deserializes it.xxxxxxxxxx// Example: Reading an Object from a Fileimport java.io.FileInputStream;import java.io.ObjectInputStream;public class DeserializeExample { public static void main(String[] args) { try { // Create an ObjectInputStream FileInputStream fileIn = new FileInputStream("student.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); // Deserialize the object Student student = (Student) in.readObject(); // Close the stream in.close(); fileIn.close(); // Display the object data System.out.println("Deserialized Student..."); System.out.println("Name: " + student.name); System.out.println("Age: " + student.age); } catch (Exception e) { e.printStackTrace(); } }}Student object previously serialized and saved in student.ser.readObject() method reads the object from the file and reconstructs it as a Student object.serialVersionUID: A unique identifier for each class that is used during deserialization to ensure that a loaded class matches the serialized object.
private static final long serialVersionUID = 1L;
transient keyword is used for fields that you don't want to serialize.transient are not included in the serialization process.Serializable is used for default serialization, you can also implement Externalizable to control the serialization process manually.