
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - ObjectInputStream readBoolean() method
Description
The Java ObjectInputStream readBoolean() method reads a single boolean value (true or false) from the stream. It is used when a boolean value has been written using writeBoolean(boolean b).
Declaration
Following is the declaration for java.io.ObjectInputStream.readBoolean() method −
public boolean readBoolean()
Parameters
NA
Return Value
This method returns the boolean read.
Exception
EOFException − If end of file is reached.
IOException − If an I/O error has occurred.
Example - Usage of ObjectInputStream readBoolean() method
The following example shows the usage of Java ObjectInputStream readBoolean() method.
ObjectInputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectInputStreamDemo { public static void main(String[] args) { try { // create a new file with an ObjectOutputStream FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeBoolean(true); oout.flush(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read and print a boolean System.out.println("" + ois.readBoolean()); } catch (Exception ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
true
Example - Writing and Reading a Boolean Value
The following example shows the usage of Java ObjectInputStream readBoolean() method. This example writes a single boolean value to a file and reads it using readBoolean().
ObjectInputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectInputStreamDemo { public static void main(String[] args) { try { // Writing a boolean value to a file FileOutputStream fos = new FileOutputStream("boolean_data.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeBoolean(true); // Writing 'true' oos.close(); // Reading boolean value using ObjectInputStream FileInputStream fis = new FileInputStream("boolean_data.dat"); ObjectInputStream ois = new ObjectInputStream(fis); boolean value = ois.readBoolean(); // Read boolean value System.out.println("Read Boolean Value: " + value); ois.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Read Boolean Value: true
Explanation
Writes true to the file using writeBoolean(true).
Reads the boolean value using readBoolean().
Prints the value, confirming that true was correctly stored and retrieved.
Example - Writing and Reading Multiple Boolean Values
The following example shows the usage of Java ObjectInputStream readBoolean() method. This example writes multiple boolean values (true and false) and reads them sequentially.
ObjectInputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectInputStreamDemo { public static void main(String[] args) { try { // Writing multiple boolean values FileOutputStream fos = new FileOutputStream("multiple_booleans.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeBoolean(true); oos.writeBoolean(false); oos.writeBoolean(true); oos.close(); // Reading boolean values using ObjectInputStream FileInputStream fis = new FileInputStream("multiple_booleans.dat"); ObjectInputStream ois = new ObjectInputStream(fis); System.out.println("First Boolean: " + ois.readBoolean()); System.out.println("Second Boolean: " + ois.readBoolean()); System.out.println("Third Boolean: " + ois.readBoolean()); ois.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
First Boolean: true Second Boolean: false Third Boolean: true
Explanation
Writes three boolean values (true, false, true) to a file.
Reads them one by one using readBoolean(), maintaining the original order.
Prints each boolean value, confirming correct serialization and deserialization.