Home »
Java programming language
Java DataInputStream readBoolean() Method with Example
DataInputStream Class readBoolean() method: Here, we are going to learn about the readBoolean() method of DataInputStream Class with its syntax and example.
Submitted by Preeti Jain, on March 30, 2020
DataInputStream Class readBoolean() method
- readBoolean() method is available in java.io package.
- readBoolean() method is used to check whether this stream read the boolean value or not.
- 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 this stream is not opened.
- EndOfFileException: This exception may throw when this stream has reached its endpoint.
Syntax:
public final boolean readBoolean();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is boolean, it returns true when byte is non-zero otherwise it returns false.
Example:
// Java program to demonstrate the example
// of boolean readBoolean() method of
// DataInputStream
import java.io.*;
public class ReadBooleanOfDIS {
public static void main(String[] args) throws Exception {
InputStream is_stm = null;
DataInputStream dis_stm = null;
byte[] b_arr = {
97,
0,
99,
100,
0,
101
};
try {
// Instantiate ByteArrayInputStream and
// DataInputStream
is_stm = new ByteArrayInputStream(b_arr);
dis_stm = new DataInputStream(is_stm);
// Loop To Read Available Data till end
while (dis_stm.available() > 0) {
// By using readBoolean() method returns true
// if the read byte is non-zero otherwise
// it returns false
boolean status = dis_stm.readBoolean();
System.out.println("dis_stm.readBoolean(): " + status);
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
// To free system resorces linked
// with these streams
if (is_stm != null)
is_stm.close();
if (dis_stm != null)
dis_stm.close();
}
}
}
Output
dis_stm.readBoolean(): true
dis_stm.readBoolean(): false
dis_stm.readBoolean(): true
dis_stm.readBoolean(): true
dis_stm.readBoolean(): false
dis_stm.readBoolean(): true