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)
java_files_io.htm
Advertisements