Java IO Tutorial - Java DataInputStream(InputStream in) Constructor








Syntax

DataInputStream(InputStream in) constructor from DataInputStream has the following syntax.

public DataInputStream(InputStream in)

Example

In the following code shows how to use DataInputStream.DataInputStream(InputStream in) constructor.

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
//  w w  w.j  a v  a  2s. c om
public class Main {
  public static void main(String[] args) throws IOException {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    for (byte j : b) {
      dos.writeByte(j);
    }
    dos.flush();

    InputStream is = new FileInputStream("c:\\test.txt");

    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
      int k = dis.read();
      System.out.print(k);
      dis.skipBytes(1);
    }

  }
}