Last Updated : 05 Jun, 2020
The
readBoolean()method of
DataInputStreamclass in Java is used to read one input byte and if the byte read is zero this method returns false and if the byte read is nonzero then this method returns true.
Syntax:public final boolean readBoolean() throws IOExceptionSpecified By:
This method is specified by readBoolean() method of
DataInputinterface.
Parameters:This method does not accept any parameter.
Return value:This method returns the boolean value read i.e true or false.
Exceptions:Below programs illustrate readBoolean() method in DataInputStream class in IO package:
Program 1: Java
// Java program to illustrate
// DataInputStream readBoolean() method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Create byte array
byte[] b = { 10, 0, 0, 20, 0 };
// Create byte array input stream
ByteArrayInputStream byteArrayInputStr
= new ByteArrayInputStream(b);
// Convert byte array input stream to
// DataInputStream
DataInputStream dataInputStr
= new DataInputStream(
byteArrayInputStr);
while (dataInputStr.available() > 0) {
// Print boolean value
System.out.println(
dataInputStr.readBoolean());
}
}
}
Output:
true false false true falseProgram 2: Java
// Java program to illustrate
// DataInputStream readBoolean() method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Create byte array
byte[] b = { 10, 5, 7, 1, 0 };
// Create byte array input stream
ByteArrayInputStream byteArrayInputStr
= new ByteArrayInputStream(b);
// Convert byte array input stream to
// DataInputStream
DataInputStream dataInputStr
= new DataInputStream(
byteArrayInputStr);
while (dataInputStr.available() > 0) {
// Print boolean value
System.out.println(
dataInputStr.readBoolean());
}
}
}
Output:
true true true true falseReferences: https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readBoolean()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4