in , ,

Java ObjectInputStream Class

Java ObjectInputStream Class
Java ObjectInputStream Class

In this tutorial, we will learn about Java ObjectInputStream and its strategies with the help of examples.

The ObjectInputStream class of the java.io package can be used to read objects that were previously written by ObjectOutputStream.

It extends the InputStream abstract class.

Before you learn about the ObjectInputStream class, make sure you know about the ObjectOutputStream Class.


Working of ObjectInputStream

The ObjectInputStream is primarily used to read data written by the ObjectOutputStream.

Fundamentally, the ObjectOutputStream changes over the Java object into comparing streams. This is known as serialization. Those changes over streams can be put away in documents or moved through networks.

Presently, if we need to read those objects, we will use the ObjectInputStream that will change over the streams back to relating objects. This is known as deserialization.


Make an ObjectInputStream

To make an object input stream, we should import the java.io.ObjectInputStream package first. When we import the package, here is the means by which we can make an input stream.

// Creates a file input stream linked with the specified file
FileInputStream fileStream = new FileInputStream(String file);

// Creates an object input stream using the file input stream
ObjectInputStream objStream = new ObjectInputStream(fileStream);

In the above example, we have made an object input stream named objStream that is connected with the document input stream named fileStream.

Presently, the objStream can be used to read objects from the file.


Methods of ObjectInputStream

The ObjectInputStream class provides implementations of different methods present in the InputStream class.

read() Method

  • read() – reads a byte of data from the input stream
  • readBoolean() – reads data in boolean form
  • readChar() – reads data in character form
  • readInt() – reads data in integer form
  • readObject() – reads the object from the input stream

Example 1: Java ObjectInputStream

Let’s see how we can use the ObjectInputStream class to read objects written by the ObjectOutputStream class.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

class Main {
    public static void main(String[] args) {

        int data1 = 5;
        String data2 = "This is worldofitech";

        try {
            FileOutputStream file = new FileOutputStream("file.txt");
            ObjectOutputStream output = new ObjectOutputStream(file);

            // Writing to the file using ObjectOutputStream
            output.writeInt(data1);
            output.writeObject(data2);

            FileInputStream fileStream = new FileInputStream("file.txt");
            // Creating an object input stream
            ObjectInputStream objStream = new ObjectInputStream(fileStream);

            //Using the readInt() method
            System.out.println("Integer data :" + objStream.readInt());

            // Using the readObject() method
            System.out.println("String data: " + objStream.readObject());

            output.close();
            objStream.close();
        }
        catch (Exception e) {
            e.getStackTrace();
        }
    }
}

Output

Integer data: 5
String data: This is worldofitech

In the above example, we have used the readInt() and readObject() strategy to read integer data and object data from the file.

Here, we have used the ObjectOutputStream to write data to the file. We at that point read the data from the file using the ObjectInputStream.


Example 2: Java ObjectInputStream

Let’s see another practical example,

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Dog implements Serializable {

    String name;
    String breed;

    public Dog(String name, String breed) {
        this.name = name;
        this.breed = breed;
    }
}

class Main {
    public static void main(String[] args) {

        // Creates an object of Dog class
        Dog dog = new Dog("Tyson", "Labrador");

        try {
            FileOutputStream file = new FileOutputStream("file.txt");

            // Creates an ObjectOutputStream
            ObjectOutputStream output = new ObjectOutputStream(file);

            // Writes objects to the output stream
            output.writeObject(dog);

            FileInputStream fileStream = new FileInputStream("file.txt");

            // Creates an ObjectInputStream
            ObjectInputStream input = new ObjectInputStream(fileStream);

            // Reads the objects
            Dog newDog = (Dog) input.readObject();

            System.out.println("Dog Name: " + newDog.name);
            System.out.println("Dog Breed: " + newDog.breed);

            output.close();
            input.close();
        }

        catch (Exception e) {
            e.getStackTrace();
        }
    }
}

Output

Dog Name: Tyson
Dog Breed: Labrador

In the above example, we have created

  • ObjectOutputStream named output using the FileOutputStream named file
  • ObjectInputStream named input using the FileInputStream named fileStream
  • An object dog of the Dog class

Here, we have then used the object output stream to write the object to the record. Also, the item input stream to read the object from the document.

Note: The Dog class executes the Serializable interface. It is on the grounds that the ObjectOutputStream just writes the serializable objects to the output stream.


Other Methods Of ObjectInputStream

MethodsDescriptions
available()returns the available number of bytes in the input stream
mark()marks the position in input stream up to which data has been read
reset()returns the control to the point in the input stream where the mark was set
skipBytes()skips and discards the specified bytes from the input stream
close()closes the object input stream

To learn more, visit Java ObjectInputStream (official Java documentation).


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java ByteArrayOutputStream Class

Java ByteArrayOutputStream Class

Java ObjectOutputStream Class

Java ObjectOutputStream Class