Tech and Media Labs
This site uses cookies to improve the user experience.




Java IO: DataInputStream

Jakob Jenkov
Last update: 2015-09-03

The Java DataInputStream class enables you to read Java primitives (int, float, long etc.) from an InputStream instead of only raw bytes. You wrap an InputStream in a DataInputStream and then you can read Java primitives from the DataInputStream. That is why it is called DataInputStream - because it reads data (numbers) instead of just bytes.

DataInputStream converts bytes to int, long, float and double from an InputStream

The DataInputStream is handy if the data you need to read consists of Java primitives larger than one byte each, like int, long, float, double etc. The DataInputStream expects the multi byte primitives to be written in network byte order (Big Endian - most significant byte first).

Often you will use a DataInputStream to read data written by a DataOutputStream.

DataInputStream Example

Here is a Java DataInputStream example:

DataInputStream dataInputStream = new DataInputStream(
                            new FileInputStream("binary.data"));

int    aByte   = input.read();
int    anInt   = input.readInt();
float  aFloat  = input.readFloat();
double aDouble = input.readDouble();
//etc.

input.close();

First a DataInputStream is created with a FileInputStream as source for its data. Second, Java primitives are read from the DataInputStream.

Using a DataInputStream With a DataOutputStream

As mentioned earlier, the DataInputStream class is often used together with a DataOutputStream. Therefore I just want to show you an example of first writing data with a DataOutputStream and then reading it again with a DataInputStream. Here is the example Java code:

import java.io.*;


public class DataInputStreamExample {

    public static void main(String[] args) throws IOException {
        DataOutputStream dataOutputStream =
                new DataOutputStream(
                        new FileOutputStream("data/data.bin"));

        dataOutputStream.writeInt(123);
        dataOutputStream.writeFloat(123.45F);
        dataOutputStream.writeLong(789);

        dataOutputStream.close();

        DataInputStream dataInputStream =
                new DataInputStream(
                        new FileInputStream("data/data.bin"));

        int   int123     = dataInputStream.readInt();
        float float12345 = dataInputStream.readFloat();
        long  long789    = dataInputStream.readLong();

        dataInputStream.close();

        System.out.println("int123     = " + int123);
        System.out.println("float12345 = " + float12345);
        System.out.println("long789    = " + long789);
    }
}

This example first creates a DataOutputStream and then writes an int, float and a long value to a file. Second the example creates a DataInputStream which reads the int, float and long value in from the same file.

Closing a DataInputStream

When you are finished reading data from the DataInputStream you should remember to close it. Closing a DataInputStream will also close the InputStream instance from which the DataInputStream is reading.

Closing a DataInputStream is done by calling its close() method. Here is how closing a DataInputStream looks:

dataInputStream.close();

You can also use the try-with-resources construct introduced in Java 7. Here is how to use and close a DataInputStream looks with the try-with-resources construct:

InputStream input = new FileInputStream("data/data.bin");

try(DataInputStream dataInputStream =
    new DataInputStream(input)){

    int data = dataInputStream.readInt();

    int   int123     = dataInputStream.readInt();
    float float12345 = dataInputStream.readFloat();
    long  long789    = dataInputStream.readLong();
}

Notice how there is no longer any explicit close() method call. The try-with-resources construct takes care of that.

Notice also that the first FileInputStream instance is not created inside the try-with-resources block. That means that the try-with-resources block will not automatically close this FileInputStream instance. However, when the DataInputStream is closed it will also close the InputStream instance it reads from, so the FileInputStream instance will get closed when the DataInputStream is closed.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC