Java.io.ObjectInputStream.readObject() Method

Advertisements


Description

The java.io.ObjectInputStream.readObject() method read an object from the ObjectInputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are read. Default deserializing for a class can be overriden using the writeObject and readObject methods. Objects referenced by this object are read transitively so that a complete equivalent graph of objects is reconstructed by readObject.

The root object is completely restored when all of its fields and the objects it references are completely restored. At this point the object validation callbacks are executed in order based on their registered priorities. The callbacks are registered by objects (in the readObject special methods) as they are individually restored.

Exceptions are thrown for problems with the InputStream and for classes that should not be deserialized. All exceptions are fatal to the InputStream and leave it in an indeterminate state; it is up to the caller to ignore or recover the stream state.

Declaration

Following is the declaration for java.io.ObjectInputStream.readObject() method

public final Object readObject()

Parameters

  • NA

Return Value

This method returns the object read from the stream

Exception

  • ClassNotFoundException -- Class of a serialized object cannot be found.

  • InvalidClassException -- Something is wrong with a class used by serialization.

  • StreamCorruptedException -- Control information in the stream is inconsistent.

  • OptionalDataException -- Primitive data was found in the stream instead of objects.

  • IOException -- Any of the usual Input/Output related exceptions.

Example

The following example shows the usage of java.io.ObjectInputStream.readObject() method.

package com.tutorialspoint;

import java.io.*;

public class ObjectInputStreamDemo {

   public static void main(String[] args) {

      String s = "Hello World";
      byte[] b = {'e', 'x', 'a', 'm', 'p', 'l', 'e'};
      try {

         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeObject(s);
         oout.writeObject(b);
         oout.flush();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois =
                 new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print an object and cast it as string
         System.out.println("" + (String) ois.readObject());

         // read and print an object and cast it as string
         byte[] read = (byte[]) ois.readObject();
         String s2 = new String(read);
         System.out.println("" + s2);


      } catch (Exception ex) {
         ex.printStackTrace();
      }

   }
}

Let us compile and run the above program, this will produce the following result:

Hello World
example



Advertisements
Advertisements