Friday, June 19, 2026
HomeLanguagesJavaObjectInputStream readBoolean() method in Java with examples

ObjectInputStream readBoolean() method in Java with examples

The readBoolean() method of the ObjectInputStream class in Java reads a boolean from the stream.

Syntax:

public boolean readBoolean()

Parameters: This method does not accept any parameter.

Return Value: This method returns the boolean that has been read.

Errors and Exceptions: The function throws two exceptions which is described below:

  • EOFException: The function is thrown if the end of file is reached.
  • IOException: The function is thrown if an I/O error has occurred.

Below program illustrate the above method:

Program 1:




// Java program to illustrate
// the above method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
    {
        try {
  
            // create a new file
            // with an ObjectOutputStream
            FileOutputStream out
                = new FileOutputStream("gopal.txt");
            ObjectOutputStream out1
                = new ObjectOutputStream(out);
  
            // write
            out1.writeUTF("Geeks for Geeks");
  
            // Flushes the stream
            out1.flush();
  
            // create an ObjectInputStream
            // for the file
            ObjectInputStream example
                = new ObjectInputStream(
                    new FileInputStream("gopal.txt"));
  
            // Read from the stream
            for (int i = 0; i < example.available();) {
                System.out.print("" + (char)example.read());
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}


Output:

Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ObjectInputStream.html#readBoolean()

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6965 POSTS0 COMMENTS