Home » Java programming language

Java ObjectOutputStream replaceObject() Method with Example

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

ObjectOutputStream Class replaceObject() method

  • replaceObject() method is available in java.io package.
  • replaceObject() method is used to permit all trusted child classes of ObjectOutputStream to substitute the given object for another while serializing. Replacing objects is deactivated until enableReplaceObject() character that the stream requesting to do replacement can be trusted. The first occurrence of every object written in the serialization stream is passed to replaceObject. Subsequent references to the objects are replaced by the object retrieved by the call to replaceObject. It allows only trusted streams to replaceObject.
  • replaceObject() 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.
  • replaceObject() method may throw an exception at the time of replacing the object.
    IOException: This exception may throw when getting any input/output error while writing to the output stream.

Syntax:

    protected Object replaceObject(Object o);

Parameter(s):

  • Object o – represents the object is to be replaced with.

Return value:

The return type of this method is Object, it returns another alternative object that replaced the given object.

Example:

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

import java.io.*;

public class ReplaceObject extends ObjectOutputStream {
 public ReplaceObject(OutputStream os_stm) throws IOException {
  super(os_stm);
 }

 public static void main(String[] args) throws IOException {
  String str = "Java World!!!";

  try {
   // Instantiates ObjectOutputStream , ObjectInputStream 
   // FileInputStream and FileOutputStream
   FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
   ReplaceObject obj_out_stm = new ReplaceObject(file_out_stm);
   FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
   ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);

   //obj_out_stm.writeObject(new ReadObject()); 
   obj_out_stm.writeUTF(str);
   obj_out_stm.flush();

   // By using enableReplaceObject() method is
   // to enable to reolace object
   System.out.println("enableReplaceObject() invoked: ");
   boolean status = obj_out_stm.enableReplaceObject(true);
   System.out.println("obj_in_stm.enableReplaceObject(true): " + status);

   Object o = obj_out_stm.replaceObject("Java Programmer");
   System.out.println("obj_out_stm.replaceObject(Java Programmer): " + o);
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }
}

Output

enableReplaceObject() invoked: 
obj_in_stm.enableReplaceObject(true): false
obj_out_stm.replaceObject(Java Programmer): Java Programmer





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.