Java - DataInputStream readByte() method



Description

The Java DataInputStream readByte() method read and returns one single input byte. The byte is a signed value in the range -128 to 127.

Declaration

Following is the declaration for java.io.DataInputStream.readByte() method −

public final byte readByte()

Parameters

NA

Return Value

The byte 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 readByte() 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 readByte() method, we're reading the byte and move the stream pointer to next byte.

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);
         
         // readByte till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readByte());
         }
         
      } 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 −

65
0
0
68
69

Example 2

The following example shows the usage of Java DataInputStream readByte() 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 readByte() method, we're reading the byte to see if this method throws exception or not. As a result, we can see the readByte() 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();
         
         // readByte till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readByte());
         }
         
      } 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 −

65
0
0
68
69

Example 3

The following example shows the usage of Java DataInputStream readByte() 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 readByte() method, we're reading the byte and move the stream pointer to next byte. Now as a special case, we're reading bytes after all bytes are read using readByte() method. As a result, we can see the readByte() 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);
         
         // readByte till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readByte());
         }
         System.out.println(dis.readByte());
      } 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 −

65
0
0
68
69
java.io.EOFException
	at java.base/java.io.DataInputStream.readByte(DataInputStream.java:272)
	at DataInputStreamDemo.main(DataInputStreamDemo.java:23)
java_files_io.htm
Advertisements