DataInputStream in Java | Methods, Example

DataInputStream in Java is a filter input stream that provides methods for reading Java’s standard data types.

It enables you conveniently to read strings and all primitive data types such as int, float, long, double, etc from a stream.

Java DataInputStream reads bytes from an underlying stream and converts them into suitable primitive-type values or strings.

It reads them to its underlying byte stream and encodes these values in a machine-independent way.

The basic input stream provides read methods only for reading bytes or characters. If we want to read the primitive data types, we need to use a filter class DataInputStream.

DataInputStream class works as wrappers on the existing input stream to filter data in the original stream.

Java DataInputStream class declaration

DataInputStream class extends FilterInputStream class that extends InputStream. It implements the interface DataInput to use methods defined in the DataInput interface.

The general declaration for DataInputStream class in Java is given below:

public class DataInputStream
     extends FilterInputStream
       implements DataInput

DataInputStream class also implements Closeable and AutoCloseable interfaces. It was added in Java 1.0 version. It is present in the package.

Constructor of DataInputStream class in Java

DataInputStream class defines only a single constructor in Java that is as follows:

1. DataInputStream​(InputStream inputStream): This constructor creates a DataInputStream object that uses the specified underlying InputStream. Here, inputStream defines the input stream from which data will be read.

A data input stream object for input can be created as follows:

FileInputStream fis = new FileInputStream(String filename);
DataInputStream dis = new DataInputStream(fis);

These two statements basically wrap dis on fis and use it as a filter. Look at the figure below.

DataInputStream class in Java

DataInputStream Methods in Java

In addition to methods inherited by InputStream and FilterInputStream superclasses, DataInputStream class uses also methods defined by DataInput interface that make it unique.

These methods read a sequence of bytes and convert them into values of primitive data types. A list of important methods provided by DataInput interface is as follows:

Methods Description
1. boolean readBoolean(): This method reads byte from the stream and converts into boolean value. It returns true if byte is non zero, false if byte is zero.
2. byte readByte() This method reads a single byte from contained input stream and returns a singed byte.
3. short readShort(): This method reads two bytes and returns a short value.
4. char readChar(): It reads two bytes from the contained input stream and returns a char value.
5. int readInt(): It reads 4 bytes from contained input stream and returns an int value.
5. long readLong(): This method read 8 bytes from the input stream and returns a long value.
6. float readFloat(): This method reads 4 bytes, converts data into float value, and returns it.
7. double readDouble(): The readDouble() method reads 8 bytes, converts data into double value, and returns it.
8. String readUTF(): This method reads the length of string and then reads and returns a string that has been encoded using the UTF-8 format.
9. int skipBytes​(int n): This method skips over a specified number of bytes without reading them from an input stream.
10. void readFully(byte[ ] b): This method reads bytes from the input stream and store them into the buffer array.
11. void readFully(byte[ ] b, int n, int m): This method reads m bytes from array b starting from nth byte.

All these methods will throw an exception named IOException when any error encounters.

Java DataInputStream Example Program

Let’s take an example program where we will write primitive data types in a file, then read data from the file, and display them on the screen. Look at the following source code below.

Program source code 1:

package javaProgram;
public class DataInputStreamEx {
public static void main(String[] args) throws IOException
 String filepath = "D:\\myfileout.dat";
// Create a FileOutputStream object to connect with myfileout.dat file.
     FileOutputStream fos = new FileOutputStream(filepath);
// Create a DataOutputStream object to wrap on fos.  
     DataOutputStream dos = new DataOutputStream(fos);

// Write following primitive data to the "myfileout.dat" file.
     dos.writeUTF("Welcome to Java world");
// Reading data from the "myfileout.dat" file.
     FileInputStream fis = new FileInputStream(filepath);
     DataInputStream dis = new DataInputStream(fis);
           Welcome to Java world

In this program, we have performed reading and writing primitive data types by wrapping DataInputStream on FileInputStream.

The program first creates “myfileout.dat” file on the mentioned filepath and then writes the string and primitive data types into it using data output stream. At the end of writing, streams are closed using close() method.

Now the program also constructs a data input stream object and connects it to “myfileout.dat” file. It then reads the following data from the file and displays them on the console. At last, it closes the streams.

Note: The main method declares that it throws an exception named IOException. Therefore, we do not use Java try-catch block.

Hope that this tutorial has covered all the important points related to DataInputStream class in Java with example program. I hope that you will have understood all the basic points of data input stream.

In the next tutorial, we will learn DataOutputStream in Java.
Thanks for reading!!!
Next ⇒ DataOutputStream in Java

⇐ Prev Next ⇒