Java IO Tutorial - Java DataInputStream








DataInputStream can read Java primitive data type values from an input stream.

The DataInputStream class contains read methods to read a value of a data type. For example, to read an int value, it contains a readInt() method; to read a char value, it has a readChar() method, etc. It also supports reading strings using the readUTF() method.

Example

The following code shows how to read primitive values and Strings from a File.

import java.io.DataInputStream;
import java.io.FileInputStream;
//  w w w  .  j a v a 2  s. c om
public class Main {
  public static void main(String[] args) {
    String srcFile = "primitives.dat";

    try (DataInputStream dis = new DataInputStream(new FileInputStream(srcFile))) {
      // Read the data in the same order they were written 
      int intValue = dis.readInt();
      double doubleValue = dis.readDouble();
      boolean booleanValue = dis.readBoolean();
      String msg = dis.readUTF();

      System.out.println(intValue);
      System.out.println(doubleValue);
      System.out.println(booleanValue);
      System.out.println(msg);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

The code above generates the following result.