Home » Java programming language

Java ObjectOutputStream writeUnshared() Method with Example

ObjectOutputStream Class writeUnshared() method: Here, we are going to learn about the writeUnshared() method of ObjectOutputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 10, 2020

ObjectOutputStream Class writeUnshared() method

  • writeUnshared() method is available in java.io package.
  • writeUnshared() method is used to write an unshared object to the ObjectOutputStream and it writes the given object as a flush distinct object in the stream.
  • writeUnshared() 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.
  • writeUnshared() method may throw an exception at the time of writing unshared object.
    • InvalidClassException: This exception may throw when something is invalid with the class of serialized objects.
    • NotSerializableException: This exception may throw when serialized object unimplement Serializable interface.
    • IOException: This exception may throw when getting any input/output error while writing to the output stream.


    public void writeUnshared(Object o);


  • Object o – represents the object to write to the stream.

Return value:

The return type of the method is void, it returns nothing.


// Java program to demonstrate the example 
// of void writeUnshared(Object o) method of 
// ObjectOutputStream

import java.io.*;

public class WriteUnsharedOfOOS {
 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 );

  // 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.writeUnshared(): " + o);


obj_in_stm.writeUnshared(): 10

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.