English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn Java ObjectOutputStream and its methods with examples.
The java.io package's ObjectInputStream class can be used to read objects written by ObjectOutputStream previously.
It extends the InputStream abstract class.
Before learning the ObjectInputStream class, make sure you understandObjectOutputStream class.
ObjectInputStream is mainly used to read data written by ObjectOutputStream.
Essentially, ObjectOutputStream converts Java objects into the corresponding stream. This is called serialization. These converted streams can be stored in files or transmitted over the network.
Now, if you need to read these objects, you will use ObjectInputStream, which will convert the stream back to the corresponding object. This is called deserialization.
To create an object input stream, we must first import the java.io.ObjectInputStream package. After importing the package, we can create the input stream.
//Create a file input stream linked to the specified file FileInputStream fileStream = new FileInputStream(String file); //Create an object input stream using a file input stream ObjectInputStream objStream = new ObjectInputStream(fileStream);
In the above example, we created an object input stream named objStream that is linked to the file input stream named fileStream.
Now, objStream can be used to read objects from the file.
The ObjectInputStream class provides different method implementations that exist in the InputStream class.
read() - Read a byte of data from the input stream
readBoolean() - Read data as a boolean
readChar() - Read data as a character
readInt() - Read data as an integer
readObject() - Read an object from the input stream
Let's see how to use the ObjectInputStream class to read objects created 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 w3codebox"; try { FileOutputStream file = new FileOutputStream("file.txt"); ObjectOutputStream output = new ObjectOutputStream(file); //Using ObjectOutputStream to write to the file output.writeInt(data1); output.writeObject(data2); FileInputStream fileStream = new FileInputStream("file.txt"); //Create 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 result
Integer data: 5 String data: This is w3codebox
In the above example, we used readInt() and readObject() methods to read integer data and object data from the file.
Here, we use ObjectOutputStream to write data to the file. Then, we use ObjectInputStream to read data from the file.
Let's look at another actual 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) { //Create an object of Dog class Dog dog = new Dog("Tyson", "Labrador"); try { FileOutputStream file = new FileOutputStream("file.txt"); //Create an ObjectOutputStream ObjectOutputStream output = new ObjectOutputStream(file); //Write object to output stream output.writeObject(dog); FileInputStream fileStream = new FileInputStream("file.txt"); //Create an ObjectInputStream ObjectInputStream input = new ObjectInputStream(fileStream); //Read object Dog newDog = (Dog) input.readObject(); System.out.println("Dog's name: ", + newDog.name); System.out.println("Dog breed: ", + newDog.breed); output.close(); input.close(); } catch (Exception e) { e.getStackTrace(); } } }
Output result
Dog's name: Tyson Dog breed: Labrador
In the above example, we created
ObjectOutputStream uses a FileOutputStream named file for output
ObjectInputStream uses a FileInputStream named fileStream for input
The object of the Dog class dog
Here, we then use the object output stream to write objects to the file. And, the object input stream reads objects from the file.
Note: The Dog class implements the Serializable interface. This is because ObjectOutputStream only writes serializable objects to the output stream.
Method | Content Description |
---|---|
available() | Return the number of available bytes in the input stream |
mark() | Mark the position of the data that has been read in the input stream |
reset() | Return the control to the point in the input stream where the mark was set |
skipBytes() | Skip and discard the specified bytes from the input stream |
close() | Close the object input stream |