The Java DataInputStream readFully(byte[] b) method reads bytes from an input stream and allocates those into the buffer array b.
It blocks until the one of the below conditions occurs −
b.length bytes of input data are available.
End of file detected.
If any I/O error occurs.
Following is the declaration for java.io.DataInputStream.readFully(byte[] b) method −
public final void readFully(byte[] b)Parameters
NA
Return ValueThis method does not return any value.
ExceptionIOException − If any I/O error occurs or the stream has been closed.
EOFException − If this input stream reaches the end before.
The following example shows the usage of Java DataInputStream readFully(byte[] b) 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 readFully() method, which populates the bytearray completely. 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 file input stream is = new FileInputStream("test.txt"); // create new data input stream dis = new DataInputStream(is); // available stream to be read int length = dis.available(); // create buffer byte[] buf = new byte[length]; // read the full data into the buffer dis.readFully(buf); // for each byte in the buffer for (byte b:buf) { // convert byte to char char c = (char)b; // prints character System.out.print(c); } } catch(Exception e) { // if any error occurs e.printStackTrace(); } finally { // releases all system resources from the streams if(is!=null) is.close(); if(dis!=null) dis.close(); } } }
Assuming we have a text file test.txt, which has the following content. This file will be used as an input for our example program −
Hello World!Output
Let us compile and run the above program, this will produce the following result −
Hello World!Example - Usage of DataInputStream readFully(byte[] b) method
The following example shows the usage of Java DataInputStream readFully(byte[] b) 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. In this example, we're using a non-existing file and see if readFully() method is throwing exception.
DataInputStreamDemo.javapackage com.tutorialspoint; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; 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("test1.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.readFully(bs); // for each byte in the buffer for (byte b:bs) { // convert byte into character char c = (char)b; // print the character System.out.print(c+" "); } } 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(); } } }
Assuming we are not having a text file test1.txt. This file will be used as an input for our example program −
OutputLet us compile and run the above program, this will produce the following result −
java.io.FileNotFoundException: test1.txt (The system cannot find the file specified) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:219) at java.base/java.io.FileInputStream.<init&t;(FileInputStream.java:157) at java.base/java.io.FileInputStream.<init&t;(FileInputStream.java:112) at DataInputStreamDemo.main(DataInputStreamDemo.java:14)Example - Usage of DataInputStream readFully(byte[] b) method
The following example shows the usage of Java DataInputStream readFully(byte[] b) 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. In this example, we're using an empty file and see if readFully() method is throwing exception or able to read an empty file successfully.
DataInputStreamDemo.javapackage com.tutorialspoint; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; 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.readFully(bs); // for each byte in the buffer for (byte b:bs) { // convert byte into character char c = (char)b; // print the character System.out.print(c+" "); } } 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(); } } }
Assuming we have a text file test.txt, which has no content.
OutputLet us compile and run the above program, this will produce the result as empty content
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