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_readbyte.htm below:

Java - DataInputStream readByte() method

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

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.

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);
         
         // 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 - Usage of DataInputStream readByte() method

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.

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();
         
         // 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 - Usage of DataInputStream readByte() method

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.

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);
         
         // 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.readUnsignedByte(DataInputStream.java:297)
   at java.base/java.io.DataInputStream.readByte(DataInputStream.java:275)
   at com.tutorialspoint.DataInputStreamDemo.main(DataInputStreamDemo.java:25)

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