The Java DataInputStream read(byte[] b, int off, int len) method reads len bytes from the contained input stream and allocate them in the buffer b starting at b[off]. The method is blocked until input data is available, an exception is thrown or end of file is detected.
DeclarationFollowing is the declaration for java.io.DataInputStream.read(byte[] b, int off, int len) method −
public final int read(byte[] b, int off, int len)Parameters
b − The byte[] into which the data is read from the input stream.
off − The start offset in the b[].
len − The maximum number of bytes read.
Total number of bytes read, else -1 if the stream has reached the end.
ExceptionIOException − If an I/O error occurs, the first byte cannot be read, or close() is invoked before this method.
NullPointerException − If b is null.
IndexOutOfBoundsException − If len is greater than b.length - off, off is negative, or len is negative
Assuming we have a text file test.txt in current directory, which has the following content. This file will be used as an input for our example programs −
ABCDEFGHExample - Usage of DataInputStream read(byte[] b, int off, int len) method
The following example shows the usage of Java DataInputStream read(byte[] b, int offset, int len) method. We've created InputStream and DataInputStream references and then initialized them with FileInputStream and DataInputStream objects. In order to initialize DataInputStream(), we requires FileInputStream object. Once objects are created, we're checking if inputStream has content using available() method. Then a bytearray of available bytes is created which is then used in DataInputStream read() method, which populates the bytearray as per given input. Then this bytearray is iterated and printed.
DataInputStreamDemo.javapackage com.tutorialspoint; import java.io.DataInputStream; import java.io.FileInputStream; 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; try { // create input stream from file input stream is = new FileInputStream("test.txt"); // create data input stream dis = new DataInputStream(is); // count the available bytes form the input stream int count = is.available(); // create buffer byte[] bs = new byte[count]; // read data into buffer dis.read(bs, 4, 3); // for each byte in the buffer for (byte b:bs) { // print the byte System.out.print(b+" "); } } 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 −
0 0 0 0 64 80 99 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0Example - Usage of DataInputStream read(byte[] b, int off, int len) method
The following example shows the usage of Java DataInputStream read(byte[] b, int offset, int len) method. We've created InputStream and DataInputStream references and then initialized them with FileInputStream and DataInputStream objects. In order to initialize DataInputStream(), we requires FileInputStream object. Once objects are created, we're checking if inputStream has content using available() method. Then a bytearray of available bytes is created which is then used in DataInputStream read() method, which populates the bytearray with an invalid length required as input. Then this bytearray is iterated and printed.
DataInputStreamDemo.javapackage com.tutorialspoint; import java.io.DataInputStream; import java.io.FileInputStream; 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; try { // create input stream from file input stream is = new FileInputStream("test.txt"); // create data input stream dis = new DataInputStream(is); // count the available bytes form the input stream int count = is.available(); // create buffer byte[] bs = new byte[count]; // read data into buffer with an invalid length dis.read(bs, 4, 10); // for each byte in the buffer for (byte b:bs) { // print the byte System.out.print(b+" "); } } 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 −
0 0 0 0 64 80 99 -41 10 61 112 -92 64 80 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0Example - Usage of DataInputStream read(byte[] b, int off, int len) method
The following example shows the usage of Java DataInputStream read(byte[] b, int offset, int len) method. We've created InputStream and DataInputStream references and then initialized them with FileInputStream and DataInputStream objects. In order to initialize DataInputStream(), we requires FileInputStream object. Once objects are created, we're checking if inputStream has content using available() method. Then a bytearray of available bytes is created which is then used in DataInputStream read() method, which populates the bytearray as per given input to get all the bytes of the stream. Then this bytearray is iterated and printed.
DataInputStreamDemo.javapackage com.tutorialspoint; import java.io.DataInputStream; import java.io.FileInputStream; 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; try { // create input stream from file input stream is = new FileInputStream("test.txt"); // create data input stream dis = new DataInputStream(is); // count the available bytes form the input stream int count = is.available(); // create buffer byte[] bs = new byte[count]; // read data into buffer dis.read(bs, 0, count); // for each byte in the buffer for (byte b:bs) { // print the byte System.out.print(b+" "); } } 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 −
64 80 99 -41 10 61 112 -92 64 80 -72 -11 -62 -113 92 41 64 80 -2 -72 81 -21 -123 31 64 81 52 122 -31 71 -82 20 64 81 99 51 51 51 51 51 64 81 -105 -82 20 122 -31 72
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