Java I/O How to - Read one byte from a file








Question

We would like to know how to read one byte from a file.

Answer

import java.io.FileInputStream;
/* ww  w .j a v a 2  s.c om*/
public class Main {
  public static void main(String[] argv) throws Exception {
    FileInputStream file = null;
    file = new FileInputStream("main.java");
    byte x = (byte) file.read();
    System.out.println(x);
    file.close();

  }
}

The code above generates the following result.