Java Language File I/O Reading from a binary file


You can read an a binary file using this piece of code in all recent versions of Java:

Java SE 1.4
File file = new File("path_to_the_file");
byte[] data = new byte[(int) file.length()];
DataInputStream stream = new DataInputStream(new FileInputStream(file));

If you are using Java 7 or later, there is a simpler way using the nio API:

Java SE 7
Path path = Paths.get("path_to_the_file");
byte [] data = Files.readAllBytes(path);