Java I/O How to - Read int from Byte Array








Question

We would like to know how to read int from Byte Array.

Answer

  /*w w  w.j ava 2 s  .c  om*/

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;

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

    try {
      DataInputStream in3 = new DataInputStream(
        new ByteArrayInputStream("a dbcde".getBytes()));
      while(true)
        System.out.print((char)in3.readByte());
    } catch(EOFException e) {
      System.err.println("End of stream");
    }


  }
}

The code above generates the following result.