Home » Java programming language

Java ObjectInputStream close() Method with Example

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

ObjectInputStream Class close() method

  • close() method is available in java.io package.
  • close() method is used to close this ObjectInputStream and free any system resources linked with this stream.
  • close() 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.
  • close() method may throw an exception at the time of closing the stream.
    IOException: This exception may throw when getting any input/output error while performing.


    public void close();


  • It does not accept any parameter.

Return value:

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


// Java program to demonstrate the example 
// of void close() method 
// of ObjectInputStream

import java.io.*;

public class CloseOfOIS {
 public static void main(String[] args) throws Exception {
  // 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 writeUTF() method is to write
  // string to the obj_out_stm stream
  obj_out_stm.writeUTF("JAVA PROGRAMMING!!");


  for (int i = 0; i < obj_in_stm.available(); i++) {
   char ch = (char) obj_in_stm.readByte();
   System.out.println("ch: " + ch + " ");

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



ch: J 
ch: A 
ch: V 
ch: A 
ch: P 
ch: R 
ch: O 
ch: G 
close() executed: 

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.