Home »
Java programming language
Java ObjectInputStream readUnshared() Method with Example
ObjectInputStream Class readUnshared() method: Here, we are going to learn about the readUnshared() method of ObjectInputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 05, 2020
ObjectInputStream Class readUnshared() method
- readUnshared() method is available in java.io package.
- readUnshared() method is used to read "non-shared" or "unshared" object from the ObjectInputStream.
- readUnshared() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
-
readUnshared() method may throw an exception at the time of reading unshared objects.
- StreamCorruptedException: This exception may throw when the control information in the stream is not consistent.
- IOException: This exception may throw when getting any input/output error while performing.
- ClassNotFoundException: This exception may throw when the serialized object Class could not exist.
- OptionalDataException: This exception may throw when unexpected primitive data found instead of objects.
Syntax:
public Object readUnshared();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is Object, it returns reference to deserialized object.
Example:
// Java program to demonstrate the example
// of Object readUnshared() method of
// ObjectInputStream
import java.io.*;
public class ReadUnsharedOfOIS {
public static void main(String[] args) throws Exception {
Integer in = new Integer(10);
// Instantiates ObjectOutputStream , ObjectInputStream
// FileInputStream and FileOutputStream
FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
ObjectOutputStream obj_out_stm = new ObjectOutputStream(file_out_stm);
FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);
// By using writeUnshared() method is to write
// unshared object to the obj_out_stm stream
obj_out_stm.writeUnshared( in );
obj_out_stm.flush();
// By using readUnshared() method is to read
// unshared object and display fields
Object o = (Object) obj_in_stm.readUnshared();
System.out.println("obj_in_stm.readUnshared(): " + o);
}
}
Output
obj_in_stm.readUnshared(): 10