A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/io/datainputstream_readboolean.htm below:

Java - DataInputStream readBoolean() method

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 Example - Usage of DataInputStream readBoolean() method

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.

DataInputStreamDemo.java
package com.tutorialspoint;

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 - Usage of DataInputStream readBoolean() method

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.

DataInputStreamDemo.java
package com.tutorialspoint;

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 - Usage of DataInputStream readBoolean() method

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.

DataInputStreamDemo.java
package com.tutorialspoint;

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_io_datainputstream.htm


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