Showing posts with label File Handling. Show all posts
Showing posts with label File Handling. Show all posts

Tuesday, March 24, 2026

What is Serialization in Java (with File Handling)?

Serialization in Java is the process of converting an object into a byte stream so that it can be stored in a file, sent over a network, or saved in a database.

👉 When we use file handling, serialization helps us persist object data into a file and later retrieve it.




🔹 Why Serialization is Used?

In real-world applications, we often need to:

  • Save object data permanently

  • Transfer objects between systems

  • Cache objects for faster access

Serialization makes all of this possible.


🔹 How Serialization Works

Java provides built-in support using:

  • Serializable (marker interface)

  • ObjectOutputStream → to write object to file

  • ObjectInputStream → to read object from file


🔹 Step 1: Make Class Serializable

import java.io.Serializable;

class Student implements Serializable {
    int id;
    String name;

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

👉 Serializable is a marker interface (no methods)


🔹 Step 2: Serialize Object (Write to File)

import java.io.*;

public class SerializeDemo {
    public static void main(String[] args) throws Exception {
        Student s = new Student(101, "John");

        FileOutputStream fos = new FileOutputStream("student.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(s);
        oos.close();
        fos.close();

        System.out.println("Object Serialized");
    }
}

🔹 Step 3: Deserialize Object (Read from File)

import java.io.*;

public class DeserializeDemo {
    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("student.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);

        Student s = (Student) ois.readObject();

        ois.close();
        fis.close();

        System.out.println(s.id + " " + s.name);
    }
}

🔹 Key Points to Remember

✔ Object is converted into byte stream
✔ Stored in file (like .ser file)
✔ Must implement Serializable
✔ Used with file handling streams


🔹 Important Concepts

🔸 transient Keyword

  • Prevents a variable from being serialized

transient int password;

🔸 serialVersionUID

  • Used to maintain version control during serialization

private static final long serialVersionUID = 1L;

🔹 Real-Time Use Cases

  • Saving user session data

  • Storing objects in files

  • Sending objects over network (RMI, APIs)

  • Caching data in applications


🔹 Serialization vs File Handling


🚀 Final Thoughts

Serialization is a powerful feature that combines object-oriented programming with file handling, allowing you to store and retrieve complete objects easily.

If you want to master Core Java concepts like Serialization with real-time projects, check out:
👉 No 1 Core JAVA Online Training in Hyderabad



Thread vs Runnable in Java: Key Differences, Best Practices & Which One to Use (2026 Guide)

Multithreading is a core part of Java —but one of the most common interview questions is: Should you use Thread or Runnable ? Thread is a ...