DataOutputStream and DataInputStream in Java

DataOutputStream class enables you to write Java primitives to OutputStream’s instead of only bytes. You wrap an OutputStream in a DataOutputStream and then you can write primitives to it. That is why it is called a DataOutputStream – because you can write int, long, float and double values to the OutputStream, and not just raw bytes.

DataInputStream, 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 via ‘ the DataInputStream. That is why it is called DataInputStream – because it reads data (numbers) instead of just bytes.

DataOutputStream vs PrintStream

  • DataOutputStream will write numeric data in a binary format, while the PrintStream will provide a human readable form.
  • DataOutputStream outputs data in its raw binary form – eg always 4 bytes for an int
    PrintStream outputs data in a human-readable character format – so an int could be anything such as “0” or “-10”
  • Use DataOutputStream when you want to store information. Use PrintStream when you want to display information.
package com.java.io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataStreamExample {

    public static void main(String[] args) throws IOException {

        FileOutputStream fileOutputStream = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;
        FileInputStream fileInputStream = null;

        try {
            fileOutputStream = new FileOutputStream("/Test/TestFolder/test-file15.bin");
            dataOutputStream = new DataOutputStream(fileOutputStream);
            dataOutputStream.writeInt(100);
            dataOutputStream.writeFloat(123.45 F);
            dataOutputStream.writeLong(1000000000 L);
            dataOutputStream.close();

            fileInputStream = new FileInputStream("/Test/TestFolder/test-file15.bin");
            dataInputStream = new DataInputStream(fileInputStream);

            System.out.println("Reading Int     = " + dataInputStream.readInt());
            System.out.println("Reading Float = " + dataInputStream.readFloat());
            System.out.println("Reading Long = " + dataInputStream.readLong());
            dataInputStream.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            // Closing the streams
            if (fileOutputStream != null)
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (fileInputStream != null)
                fileInputStream.close();
            if (dataOutputStream != null)
                dataOutputStream.close();
            if (dataInputStream != null)
                dataInputStream.close();
        }
    }
}
Reading Int = 100
Reading Float = 123.45
Reading Long = 1000000000

Leave a Reply

Your email address will not be published. Required fields are marked *