【问题标题】:Java, Search for a long in the binary file inputJava,在二进制文件输入中搜索long
【发布时间】:2013-05-31 20:57:47
【问题描述】:
Public static void main(String[] args) {
  File inFile = null;
  if (0 < args.length) {
      inFile = new File(args[0]);
  }

    BufferedInputStream bStream = null;

    try {

        int read;
        bStream = new BufferedInputStream(new FileInputStream(inFile));

        while ((read = bStream.read()) > 0) {
            getMarker(read, bStream);
            System.out.println(read);
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    } 

    finally {
        try {
            if (bStream != null)bStream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

private static void getMarker(int read, BufferedInputStream bStream) {

}

我想在 bufferedInputStream 中找到长的 1234567890。我可以在 bufferedInputStream 中搜索长类型吗? (我不确定我是否需要“读取”作为参数。我对此表示怀疑,我可能会删除它)。如何搜索 bufferedInputStream?

【问题讨论】:

  • A long 的大小为 8 个字节。你的文件是 8 字节对齐的吗?
  • 更重要的是,根据文件中的 long 是写成 little endian 还是 big endian,其二进制表示会有所不同。那么,什么是什么?

标签: java buffer


【解决方案1】:

如果数字是 8 字节对齐且大端,则可以使用 DataInputStream。

如果不是,我建议内存映射文件并使用 ByteBuffer.order 将顺序更改为小端。这将允许您以任何您希望的方式访问该文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 2013-06-01
    相关资源
    最近更新 更多