Reading a binary file into a byte array

Use BufferedInputStream as shown in following example.

Main.java:

import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      if (args.length != 1) {
         System.err.println("Usage: java Main <file>");
         System.exit(1);
      }
 
      try {
         File f = new File(args[0]);
         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
         byte[] buffer = new byte[(int) f.length()];
         bis.read(buffer, 0, buffer.length);
         bis.close();
         System.out.println("Read file with " + buffer.length + " bytes.");
      }
      catch(IOException e) {
         System.err.println(e);
      }
   }
}