Home » Java programming language

Java ObjectOutputStream enableReplaceObject() Method with Example

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

ObjectOutputStream Class enableReplaceObject() method

  • enableReplaceObject() method is available in java.io package.
  • enableReplaceObject() method is used to activate this ObjectOutputStream to do replacement of objects in the stream. If the boolean variable is set to true then replaceObject() is invoked for every object getting serialized and it first invokes checkPermission() of SecurityManager with SerializablePermission(“enableSubstitution”) permission to verify it is ready to enable the stream to do replacement of objects in the stream when SecurityManager exists.
  • enableReplaceObject() 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.
  • enableReplaceObject() method may throw an exception at the time of enabling an object to do the replacement.
    SecurityManager: This exception may throw when its checkPermission() method does not allow to enable the stream to do replacement of objects when SecurityManager exists.

Syntax:

    protected boolean enableReplaceObject(boolean status);

Parameter(s):

  • boolean status – represents the status whether to enable or disable the stream to do replacement of objects in the stream.

Return value:

The return type of this method is boolean, it returns the old settings before this method was called.

Example:

// Java program to demonstrate the example 
// of void drain() method of
// ObjectOutputStream

import java.io.*;

public class Drain extends ObjectOutputStream {
 public Drain(OutputStream os) throws IOException {
  super(os);
 }

 public static void main(String[] args) throws Exception {
  // Instantiates ObjectOutputStream , ObjectInputStream 
  // FileInputStream and FileOutputStream
  FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
  Drain obj_out_stm = new Drain(file_out_stm);
  FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
  ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);

  // By using writeObject() method is to
  // write the object to the stream		
  obj_out_stm.writeInt(156924);

  // By using drain() method is to
  // drain the stream
  obj_out_stm.drain();

  // By using readObject() method is to 
  // read the object
  int in = (int) obj_in_stm.readInt();
  System.out.println("obj_in_stm.readInt(): " + in );

  // By using close() method is to 
  // close all the streams 
  System.out.println("Stream Shutdown... ");

  file_in_stm.close();
  file_out_stm.close();
  obj_in_stm.close();
  obj_out_stm.close();
 }
}

Output

obj_in_stm.readInt(): 156924
Stream Shutdown... 





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.