Home » Java programming language

Java ObjectInputStream readBoolean() Method with Example

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

ObjectInputStream Class readBoolean() method

  • readBoolean() method is available in java.io package.
  • readBoolean() method is used to read in a boolean from this ObjectInputStream.
  • readBoolean() 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.
  • readBoolean() method may throw an exception at the time of reading boolean.
    • IOException: This exception may throw when getting any input/output error while performing.
    • EOFException: This exception may throw when this stream has reached its end.

Syntax:

    public boolean readBoolean();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is boolean, it returns the boolean value read.

Example:

// Java program to demonstrate the example 
// of boolean  readBoolean() method of 
// ObjectInputStream

import java.io.*;

public class ReadBooleanOfOIS {
 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 writeBoolean() method is to write
  // boolean to the obj_out_stm stream
  obj_out_stm.writeBoolean(false);
  obj_out_stm.writeBoolean(true);
  obj_out_stm.flush();

  while (obj_in_stm.available() > 0) {
   boolean status = obj_in_stm.readBoolean();
   System.out.println("obj_in_stm.readBoolean():" + status);
  }

  // By using close() method is to 
  // close all the streams 
  file_in_stm.close();
  file_out_stm.close();
  obj_in_stm.close();
  obj_out_stm.close();
 }
}

Output

obj_in_stm.readBoolean():false
obj_in_stm.readBoolean():true



Comments and Discussions!

Load comments ↻






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