【问题标题】:Read any file as a binary string将任何文件作为二进制字符串读取
【发布时间】:2015-07-24 01:13:23
【问题描述】:

正如标题所暗示的那样,有没有办法用 Java(或任何其他语言)读取给定文件(.txt、.docx、.exe 等)的二进制表示?

在java中,我知道如何按原样读取文件的内容,即:

String line;
BufferedReader br = new BufferedReader(new FileReader("myFile.txt"));
while ((line = br.readLine()) != null) {
    System.out.println(line);
}

但我不确定(如果可能的话)读取文件本身的二进制表示。

【问题讨论】:

标签: java binary binaryfiles


【解决方案1】:
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 是一个字节数组,其中包含文件的所有数据

【讨论】:

猜你喜欢
  • 2010-09-12
  • 1970-01-01
  • 2013-09-26
  • 2021-11-09
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 2023-03-18
  • 2014-06-18
相关资源
最近更新 更多