您可以在所有最新版本的 Java 中使用这段代码读取二进制文件:
File file = new File("path_to_the_file"); byte[] data = new byte[(int) file.length()]; DataInputStream stream = new DataInputStream(new FileInputStream(file)); stream.readFully(data); stream.close();
如果您使用的是 Java 7 或更高版本,则有一种更简单的方法使用nio API:
Path path = Paths.get("path_to_the_file"); byte [] data = Files.readAllBytes(path);