English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about Java ObjectOutputStream and its methods with the help of examples.
The java.io package's ObjectOutputStream class can be used to write objects that can be read by ObjectInputStream.
It inherits the OutputStream abstract class.
基本上,ObjectOutputStream uses class names and object values to encode Java objects. And, therefore, generate the corresponding streams. This process is called serialization.
These converted streams can be stored in files and can be transmitted between networks.
NoteThe ObjectOutputStream class only writes those objects that implement the Serializable interface. This is because objects need to be serialized when written to the stream.
To create an object output stream, we must first import the java.io.ObjectOutputStream package. After importing the package, we can create the output stream.
//Create a FileOutputStream to write objects into ObjectOutputStream FileOutputStream fileStream = new FileOutputStream(String file); //Create ObjectOutputStream ObjectOutputStream objStream = new ObjectOutputStream(fileStream);
In the above example, we created an object output stream named objStream, which is linked to a file output stream named fileStream.
The ObjectOutputStream class provides implementations for different methods that appear in the OutputStream class.
write() - Write byte data to the output stream
writeBoolean() - Write data in boolean form
writeChar() - Write data in character form
writeInt() - Write data in integer form
writeObject() - Write object to output stream
Let's see how to use ObjectOutputStream to store objects in a file and how to use ObjectInputStream to read these objects from the file
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"); //Create an ObjectOutputStream ObjectOutputStream output = new ObjectOutputStream(file); //Write object to output stream output.writeInt(data1; output.writeObject(data2; //Using ObjectInputStream to read data 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 Result
Integer data: 5 String data: This is w3codebox
In the above example, we used the readInt() method and readObject() method to read integer and object data from the file.
Here, we use ObjectOutputStream to write data to a file. Then we use ObjectInputStream to read data from the file.
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) { //Create an object of the Dog class Dog dog1 = new Dog("Terry", "Labrador Retriever"); try { FileOutputStream fileOut = new FileOutputStream("file.txt"); //Create an ObjectOutputStream ObjectOutputStream objOut = new ObjectOutputStream(fileOut); //Write object to output stream objOut.writeObject(dog1; //Read object FileInputStream fileIn = new FileInputStream("file.txt"); ObjectInputStream objIn = new ObjectInputStream(fileIn); // Read object 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 Result
Dog name: Terry Dog breed: Labrador Retriever
In the above example, we created
The ObjectOutputStream named objOut uses the FileOutputStream named fileOut.
The ObjectInputStream named objIn uses the FileInputStream named fileIn.
The object of the Dog class, dog1.
Here, we then use the object output stream to write objects to the file. And, the object input stream reads objects from the file.
NoteThe Dog class implements the Serializable interface. This is because ObjectOutputStream only writes objects that can be serialized into the output stream.
Method | Content Description |
---|---|
flush() | Clear all data in the output stream |
drain() | Put all buffered data into the output stream |
close() | Close the Output Stream |