Java ObjectOutputStream ObjectInputStream

In this section we will discuss about the ObjectOutputStream and ObjectInputStream in Java.

Java ObjectOutputStream ObjectInputStream

In this section we will discuss about the ObjectOutputStream and ObjectInputStream in Java.

Java ObjectOutputStream ObjectInputStream

Java ObjectOutputStream ObjectInputStream

In this section we will discuss about the ObjectOutputStream and ObjectInputStream in Java.

In Java to handle the input and output of Java Objects i.e. writing and reading of Java Objects two classes are provided in the java.io package. java.io.ObjectOutputStream and java.io.ObjectInputStream are the classes used for writing and reading the corresponding Java Objects respectively. Graph of Java Objects which has support of java.io.Serializable interface can only be written to the stream using ObjectOutputStream. ObjectOutputStream can also write the primitive data. To read these Java Objects and primitive data types the corresponding ObjectInputStream is required this class reads the primitive data and Java Objects as the same type and in their order. Using both of these classes a persistent storage of data can be provided to the application with the help of FileOutputStream and FileInputStream classes. ObjectOutputStream class has a writeObject method which writes the Java Objects and the ObjectInputStream has a readObject method which reads that corresponding Java Objects. Besides these methods both of the classes have more methods which are used to write into and read the data from stream.

Example

Here an example is being given where both of the above classes are used in different Java classes as per their requirement. This example is being given for you which demonstrates that how to write Java objects into a file and read that corresponding objects from the file. In this example you will see that a class is created for writing the Java objects into the file and the another class is created for reading that corresponding objects from the file. For writing the Java objects to the file we will use the java.io.FileOutpuStream and to read that object we will use java.io.FileInputStream.

Source Code

WriteObject.java

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class WriteObject
{
    public static void main(String args[])
     {
        
        ObjectOutputStream oos = null;
        
        try
          {
             FileOutputStream fos = new FileOutputStream("empDetail.txt");
            
             oos = new ObjectOutputStream(fos);
             
             oos.writeObject("Ankit");
             oos.writeObject("Content Writer");
             oos.writeInt(8000);

             oos.writeObject("Ankit");
             oos.writeObject("Java Developer");
             oos.writeInt(20000);
            
            System.out.println();
            System.out.println("objects written to the stream successfully");
        }
     catch (FileNotFoundException fex)
       {
            System.out.println(fex);
       }
     catch (Exception ex)
      {
            System.out.println(ex);
      }
     finally
      {
          try
           {
                if (oos != null)
                 {
                     oos.close();
                 }
            }
         catch (Exception ex1)
           {
                System.out.println(ex1);
            }
       }
    }       
}

ReadObject.java

import java.io.*;

public class ReadObject
{    
    public static void main(String args[])
     {
        
        ObjectInputStream ois = null;
        
        try
          {
             FileInputStream fis = new FileInputStream("empDetail.txt");
             ois = new ObjectInputStream(fis);
            
             String sh = (String) ois.readObject();
             String cw = (String) ois.readObject();
             int      sal = ois.readInt();
             
             String am = (String) ois.readObject();
             String cw1 = (String) ois.readObject();
             int      sal1 = ois.readInt();
             System.out.println();
             System.out.println("Name \t"+"Department \t"+"Salary");
             System.out.println(sh+"\t"+cw+"\t"+sal);
             System.out.println(am+"\t"+cw1+"\t"+sal1);             
         
          }
       catch (EOFException ex)
         {           
            System.out.println("End of file reached.");
         }
       catch (ClassNotFoundException ex)
        {
            System.out.println(ex);
        }
       catch (Exception ex)
        {
            System.out.println(ex);
        }
       finally
        {
            try 
              {
                  if (ois != null)
                   {
                      ois.close();
                   }
              }
            catch (Exception ex) 
              {
                  System.out.println(ex);
              }
         }
    }
}

Output :

To read the objects from the stream it should be required that the objects must be written previously to the stream. So, first compile and execute the WriteObject.java class which will write the Java objects to the stream and then compile and execute the ReadObject.java class which will read the previously written Java objects from the stream.

I have compiled and executed my Java program exactly the same as discussed above then the output is as follows :

1. Compiled and Executed the Java program WriteObject.java then the output is as follows :

2. Now compiled and executed the Java program ReadObject.java then the output is as follows :

Download Source Code