DataInputStream readBoolean() method in Java with Examples
The readBoolean() method of DataInputStream class 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 IOException
Specified By: This method is specified by readBoolean() method of DataInput interface.
Parameters: This method does not accept any parameter.
Return value: This method returns the boolean value read i.e true or false.
Exceptions:
- EOFException – It throws EOFException if the input stream is ended.
- IOException – This method throws IOException if the stream is closed or some other I/O error occurs.
Below programs illustrate readBoolean() method in DataInputStream class in IO package:
Program 1:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
byte [] b = { 10 , 0 , 0 , 20 , 0 };
ByteArrayInputStream byteArrayInputStr
= new ByteArrayInputStream(b);
DataInputStream dataInputStr
= new DataInputStream(
byteArrayInputStr);
while (dataInputStr.available() > 0 ) {
System.out.println(
dataInputStr.readBoolean());
}
}
}
|
Output:
true
false
false
true
false
Program 2:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
byte [] b = { 10 , 5 , 7 , 1 , 0 };
ByteArrayInputStream byteArrayInputStr
= new ByteArrayInputStream(b);
DataInputStream dataInputStr
= new DataInputStream(
byteArrayInputStr);
while (dataInputStr.available() > 0 ) {
System.out.println(
dataInputStr.readBoolean());
}
}
}
|
Output:
true
true
true
true
false
References:
https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readBoolean()
Last Updated :
05 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...