in , ,

Java ObjectOutputStream Class

Java ObjectOutputStream Class
Java ObjectOutputStream Class

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

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

It extends the OutputStream abstract class.


Working of ObjectOutputStream

Fundamentally, the ObjectOutputStream encodes Java objects using the class name and object values. Furthermore, subsequently creates comparing streams. This cycle is known as serialization.

Those changes over streams can be stored in files and can be moved among networks.

Note: The ObjectOutputStream class just writes those objects that execute the Serializable interface. This is on the grounds that items should be serialized while keeping in touch with the stream


Make an ObjectOutputStream

To make an object output stream, we should import the java.io.ObjectOutputStream package first. When we import the package, here is the way we can make an output stream.

// Creates a FileOutputStream where objects from ObjectOutputStream are written
FileOutputStream fileStream = new FileOutputStream(String file);

// Creates the ObjectOutputStream
ObjectOutputStream objStream = new ObjectOutputStream(fileStream);

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


Methods of ObjectOutputStream

The ObjectOutputStream class provides implementations for different methods present in the OutputStream class.

write() Method

  • write() – writes a byte of data to the output stream
  • writeBoolean() – writes data in boolean form
  • writeChar() – writes data in character form
  • writeInt() – writes data in integer form
  • writeObject() – writes object to the output stream

Example 1: Java ObjectOutputStream

We should perceive how we can use ObjectOutputStream to store objects in a file and ObjectInputStream to read those objects from the files

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");

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

            // writes objects to output stream
            output.writeInt(data1);
            output.writeObject(data2);

            // Reads data using the ObjectInputStream
            FileInputStream fileStream = new FileInputStream("file.txt");
            ObjectInputStream objStream = new ObjectInputStream(fileStream);

            System.out.println("Integer data :" + objStream.readInt());
            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() strategy and readObject() technique to read integer data and object data from the files.

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


Example 2: Java ObjectOutputStream

Let’s take another 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 dog1 = new Dog("Tyson", "Labrador");

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

            // Creates an ObjectOutputStream
            ObjectOutputStream objOut = new ObjectOutputStream(fileOut);

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

            // Reads the object
            FileInputStream fileIn = new FileInputStream("file.txt");
            ObjectInputStream objIn = new ObjectInputStream(fileIn);

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

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

            objOut.close();
            objIn.close();
        }

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

Output

Dog Name: Tyson
Dog Breed: Labrador

In the above example, we have created

  • ObjectOutputStream named objOut using the FileOutputStream named fileOut
  • ObjectInputStream named objIn using the FileInputStream named fileIn.
  • An object dog1 of the Dog class.

Here, we have then used the object output stream to write the object to the record. What’s more, the item input stream to read the object from the file.

Note: The Dog class implements the Serializable interface. It is because the ObjectOutputStream only writes objects that can be serialized to the output stream.


Other Methods Of ObjectOutputStream

MethodsDescriptions
flush()clears all the data from the output stream
drain()puts all the buffered data in the output stream
close()closes the output stream

To learn more, visit Java ObjectOutputStream (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

2 Comments

Leave a Reply
  1. I found your weblog website on google and check just a few of your early posts. Proceed to keep up the superb operate. I just extra up your RSS feed to my MSN Information Reader. In search of forward to studying more from you afterward!…

Leave a Reply

Java ObjectInputStream Class

Java ObjectInputStream Class

Java BufferedInputStream Class

Java BufferedInputStream Class