Friday, 23 September 2016

Java Tutorial : Java IO (Java Serialization_V4)

🚀 Enjoying Java Tutorials?

Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

🔔 Subscribe to Ram N Java

Introduction to Getting Started with Java Serialization

Serialization is one of the most essential building blocks of the Java IO framework. Whether you want to persist application state across restarts or transmit live objects over a network connection, getting started with serialization allows you to convert any supported Java object into a portable stream of bytes.

How to Serialize an Object: Step-by-Step

1. Implement the Serializable Interface

Your class must implement java.io.Serializable. This is a marker interface with no abstract methods to implement, acting purely as an opt-in signal to the Java Virtual Machine.

public class Student implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private String name;

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

2. Writing the Object to a Stream (Serialization)

Use FileOutputStream wrapped inside an ObjectOutputStream to write the instance state to disk:

try (FileOutputStream fos = new FileOutputStream("student.ser");
     ObjectOutputStream oos = new ObjectOutputStream(fos)) {
    
    Student student = new Student(101, "Ram");
    oos.writeObject(student);
    System.out.println("Object serialized successfully!");
} catch (IOException e) {
    e.printStackTrace();
}

3. Reading the Object Back (Deserialization)

To recreate the object instance in memory, wrap a FileInputStream with an ObjectInputStream and cast the returned object:

try (FileInputStream fis = new FileInputStream("student.ser");
     ObjectInputStream ois = new ObjectInputStream(fis)) {
    
    Student student = (Student) ois.readObject();
    System.out.println("Deserialized student: " + student);
} catch (IOException | ClassNotFoundException e) {
    e.printStackTrace();
}

Key Points to Keep in Mind

  • serialVersionUID: Always declare an explicit serialVersionUID to avoid class version mismatch errors during deserialization.
  • transient Fields: Use transient to omit sensitive values like passwords or tokens from the output byte stream.
  • Resource Management: Always close your streams (preferably using try-with-resources) to prevent resource leaks.

Related Tutorials from Ram N Java


Click here to watch in Youtube :
https://www.youtube.com/watch?v=HhzX0CWq2zU&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java IO (Java Serialization_V4)
Java Tutorial : Java IO (Java Serialization_V4)
Java Tutorial : Java IO (Java Serialization_V4)
Java Tutorial : Java IO (Java Serialization_V4)
See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment

    Tutorials