Java FileInputStream read byte array from file

Description

Java FileInputStream read byte array from file

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
  public static void main(String[] argv) {
    try {/* w  w  w  .ja  v  a  2  s  .  co m*/
      File file = new File("Main.java");
      InputStream in = new FileInputStream(file);
      byte[] b = new byte[(int) file.length()];
      int len = b.length;
      int total = 0;

      while (total < len) {
        int result = in.read(b, total, len - total);
        System.out.println(new String(b));
        if (result == -1) {
          break;
        }
        total += result;
      }
      in.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}



PreviousNext

Related