Streams and Serialization

In this exercise, you will learn how to use **Streams and Serialization in Java** to manage data streams and convert objects to byte sequences. You will implement file reading and writing using **ObjectOutputStream** and **ObjectInputStream**, enabling efficient data storage and retrieval. This concept is essential for managing persistence in Java applications.

Topic

File Handling and Advanced Exceptions

Java Exercise

In this exercise, you will create a Java program that demonstrates the use of **Streams and Serialization** to save and retrieve objects from a file. You will implement a serializable Person class, allowing you to store its data in a file using **ObjectOutputStream** and retrieve it using **ObjectInputStream**. This technique is fundamental to data persistence in Java applications.

Instructions:

  1. Create a class named Person with attributes such as name and age, implementing the Serializable interface.
  2. Implement a method to **save an object** of type Person to a file using **ObjectOutputStream**.
  3. Implement another method to **read the object** from the file using **ObjectInputStream**.
  4. In the main method, create a Person object, save it to a file, and then retrieve it to display its data.

This exercise will help you understand how **Streams and Serialization** enable efficient data handling in Java applications. facilitating the storage and retrieval of information in files.


import java.io.*;

// Person class implementing Serializable
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;
    }

    public void displayData() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

public class SerializationDemo {
    public static void main(String[] args) {
        String file = "person.dat";

        // Create a Person object
        Person person = new Person("John", 30);

        // Serialize the object and save it to a file
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
            out.writeObject(person);
            System.out.println("Person object serialized successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Deserialize the object and read it from the file
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
            Person deserializedPerson = (Person) in.readObject();
            System.out.println("Person object deserialized:");
            deserializedPerson.displayData();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

 Output:

Successfully serialized Person object.
Deserialized Person object:
Name: John, Age: 30

This program demonstrates how to work with files in Java using the File, FileReader, and FileWriter classes. First, a file named example.txt is created, written to using FileWriter, and then the contents are read using FileReader. When the program is run, the file contents are displayed in the console, providing an understanding of how to perform basic file input/output operations in Java.


 Share this JAVA exercise