【问题标题】:How to read a bin file to a byte array?如何将 bin 文件读入字节数组?
【发布时间】:2009-10-02 20:09:36
【问题描述】:

我有一个需要转换为字节数组的 bin 文件。谁能告诉我该怎么做?

这是我目前所拥有的:

File f = new File("notification.bin");
is = new FileInputStream(f);

long length = f.length();

/*if (length > Integer.MAX_VALUE) {
    // File is too large
}*/

// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
    offset += numRead;
}

// Ensure all the bytes have been read in
if (offset < bytes.length) {
    throw new IOException("Could not completely read file "+f.getName());
}

但它不起作用......

卡迪

【问题讨论】:

  • 我为你解决了这个问题。请尝试实际格式化您发布的问题,以便它们智能且清晰。
  • 在什么情况下它不起作用?您使用的 File 类是什么?为什么不使用标准的 std::ifstream?
  • 可能是因为它是 C# 而不是 C++
  • 那不是 C# - 我相信是 Java。
  • 无论如何,它看起来确实更像 Java 而不是 C#。我已经相应地编辑了标签。

标签: java file-io bytearray


【解决方案1】:

试试这个

public byte[] readFromStream(InputStream inputStream) throws Exception
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    byte[] data = new byte[4096];
    int count = inputStream.read(data);
    while(count != -1)
    {
        dos.write(data, 0, count);
        count = inputStream.read(data);
    }

    return baos.toByteArray();
}

顺便说一句,您想要 Java 代码还是 C++ 代码。看到你问题中的代码,我认为它是一个 java 代码,因此给出了一个 java 答案

【讨论】:

  • DataOutputStream 是关于什么的?
  • +1 这很好,Tom,在这种情况下,ByteArrayOutputStream 就足够了,我们只需读取字节并写入字节。如果我们要编写原始类型,可能还需要 DataOutputStream
【解决方案2】:

使用内存映射文件可能会更好。见this question

【讨论】:

    【解决方案3】:

    在 Java 中,一个简单的解决方案是:

    InputStream is = ...
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    byte[] data = new byte[4096];  // A larger buffer size would probably help
    int count; 
    while ((count = is.read(data)) != -1) {
        os.write(data, 0, count);
    }
    byte[] result = os.toByteArray();
    

    如果输入是一个文件,我们可以预先分配一个合适大小的字节数组:

    File f = ...
    long fileSize = f.length();
    if (fileSize > Integer.MAX_VALUE) {
        // file too big
    }
    InputStream is = new FileInputStream(f);
    byte[] data = new byte[fileSize];
    if (is.read(data)) != data.length) {
        // file truncated while we were reading it???
    }
    

    但是,使用 NIO 可能有一种更有效的方法来完成这项任务。

    【讨论】:

      【解决方案4】:

      除非你真的需要这样做,否则可能会简化你正在做的事情。

      在 for 循环中执行所有操作可能看起来是一种非常巧妙的方法,但是当您需要调试并且没有立即看到解决方案时,它会让自己陷入困境。 p>

      【讨论】:

        【解决方案5】:

        在这个 answer 我从一个 URL 读取

        您可以修改它,使 InputStream 来自 File 而不是 URLConnection。

        类似:

            FileInputStream inputStream = new FileInputStream("your.binary.file");
        
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte [] buffer               = new byte[ 1024 ];
        
            int n = 0;
            while (-1 != (n = inputStream.read(buffer))) {
               output.write(buffer, 0, n);
            }
            inputStream.close();
        

        【讨论】:

          【解决方案6】:

          试试开源库apache commons-io IOUtils.toByteArray(inputStream) 您不是第一个也不是最后一个需要读取文件的开发人员,无需每次都重新发明它。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-07-08
            相关资源
            最近更新 更多