Java 以二进制字符串形式读取任何文件
正如标题所示,有没有办法用Java(或任何其他语言)读取给定文件(.txt、.docx、.exe等)的二进制表示形式 在java中,我知道如何按原样读取文件内容,即:Java 以二进制字符串形式读取任何文件,java,binary,binaryfiles,Java,Binary,Binaryfiles,正如标题所示,有没有办法用Java(或任何其他语言)读取给定文件(.txt、.docx、.exe等)的二进制表示形式 在java中,我知道如何按原样读取文件内容,即: String line; BufferedReader br = new BufferedReader(new FileReader("myFile.txt")); while ((line = br.readLine()) != null) { System.out.println(line); } 但我不确定(如果可
String line;
BufferedReader br = new BufferedReader(new FileReader("myFile.txt"));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
但我不确定(如果可能的话)是否读取文件本身的二进制表示形式
File file = new File(filePath);
byte[] bytes = new byte[(int)file.length()];
DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
dataInputStream.readFully(bytes);
dataInputStream.close();
bytes
是一个字节数组,其中包含文件的所有数据您的问题的可能重复项不清楚。您是否在询问如何将文件读取为字节数组?是否要将文件读取为完全由“0”和“1”字符组成的字符串?或者,只需使用。