- Java.io package classes
- 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 extras
- Java.io - Interfaces
- Java.io - Exceptions
- Java.io package Useful Resources
- Java.io - Discussion
Java - DataInputStream readBoolean() method
Description
The Java DataInputStream readBoolean() method returns true if the byte is non zero, false if the byte is zero.
Declaration
Following is the declaration for java.io.DataInputStream.readBoolean() method −
public final boolean readBoolean()
Parameters
NA
Return Value
The boolean value read
Exception
IOException − If the stream is closed and the contained input stream does not support reading after close, or another I/O error occurs.
EOFException − If the input stream has reached the end.
Example 1
The following example shows the usage of Java DataInputStream readBoolean() method. We've created InputStream and DataInputStream references and then initialized them with ByteArrayInputStream (populated using a byte array) and DataInputStream objects. In order to initialize DataInputStream(), we requires ByteArrayInputStream object. Once objects are created, we're checking if DataInputStream has content using available() method. Then using readBoolean() method, we're reading the byte as boolean and move the stream pointer to next byte. If byte is non-zero, then true is returned and printed else false is returned and printed.
import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; public class DataInputStreamDemo { public static void main(String[] args) throws IOException { InputStream is = null; DataInputStream dis = null; byte[] buf = {65, 0, 0, 68, 69}; try { // create new byte array input stream is = new ByteArrayInputStream(buf); // create data input stream dis = new DataInputStream(is); // readBoolean till the data available to read while( dis.available() >0) { System.out.println(dis.readBoolean()); } } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases any associated system files with this stream if(is!=null) is.close(); if(dis!=null) dis.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
true false false true true
Example 2
The following example shows the usage of Java DataInputStream readBoolean() method. We've created InputStream and DataInputStream references and then initialized them with ByteArrayInputStream (populated using a byte array) and DataInputStream objects. In order to initialize DataInputStream(), we requires ByteArrayInputStream object. Once objects are created, we're checking if DataInputStream has content using available() method. Now as a special case, we're closing the stream and then using readBoolean() method, we're reading the byte as boolean to see if this method throws exception or not. As a result, we can see the readBoolean() ignores the close() method call as underlying input stream supports read after close.
import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; public class DataInputStreamDemo { public static void main(String[] args) throws IOException { InputStream is = null; DataInputStream dis = null; byte[] buf = {65, 0, 0, 68, 69}; try { // create new byte array input stream is = new ByteArrayInputStream(buf); // create data input stream dis = new DataInputStream(is); // close the stream before reading. is.close(); dis.close(); // readBoolean till the data available to read while( dis.available() >0) { System.out.println(dis.readBoolean()); } } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases any associated system files with this stream if(is!=null) is.close(); if(dis!=null) dis.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
true false false true true
Example 3
The following example shows the usage of Java DataInputStream readBoolean() method. We've created InputStream and DataInputStream references and then initialized them with ByteArrayInputStream (populated using a byte array) and DataInputStream objects. In order to initialize DataInputStream(), we requires ByteArrayInputStream object. Once objects are created, we're checking if DataInputStream has content using available() method.
Then using readBoolean() method, we're reading the byte as boolean and move the stream pointer to next byte. If byte is non-zero, then true is returned and printed else false is returned and printed. Now as a special case, we're reading bytes after all bytes are read using readBoolean() method. As a result, we can see the readBoolean() throws an EOFException.
import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; public class DataInputStreamDemo { public static void main(String[] args) throws IOException { InputStream is = null; DataInputStream dis = null; byte[] buf = {65, 0, 0, 68, 69}; try { // create new byte array input stream is = new ByteArrayInputStream(buf); // create data input stream dis = new DataInputStream(is); // readBoolean till the data available to read while( dis.available() >0) { System.out.println(dis.readBoolean()); } System.out.println(dis.readBoolean()); } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases any associated system files with this stream if(is!=null) is.close(); if(dis!=null) dis.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
true false false true true java.io.EOFException at java.base/java.io.DataInputStream.readBoolean(DataInputStream.java:249) at DataInputStreamDemo.main(DataInputStreamDemo.java:23)