java 如何将bin文件读取到字节数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1511559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:50:47  来源:igfitidea点击:

How to read a bin file to a byte array?

javafile-iobytearray

提问by Kaddy

I have a bin file that I need to convert to a byte array. Can anyone tell me how to do this?

我有一个需要转换为字节数组的 bin 文件。谁能告诉我如何做到这一点?

Here is what I have so far:

这是我到目前为止所拥有的:

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());
}

But it's not working...

但它不起作用...

Kaddy

卡迪

回答by Prabhu R

try using this

尝试使用这个

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();
}

Btw, do you want a Java code or C++ code. Seeing the code in your question, I assumed it to be a java code and hence gave a java answer to it

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

回答by Jherico

You're probably better off using a memory mapped file. See this question

您最好使用内存映射文件。看到这个问题

回答by Stephen C

In Java, a simple solution is:

在 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();

If the input is a file, we can preallocate a byte array of the right size:

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

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???
}

However, there is probably a more efficient way to do this task using NIO.

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

回答by OscarRyz

In this answerI read from an URL

在这个答案中,我从一个 URL 中读取

You could modify it so the InputStream is from a File instead of a URLConnection.

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

Something like:

就像是:

    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();

etc

等等

回答by Pavel Feldman

Try open source library apache commons-ioIOUtils.toByteArray(inputStream)You are not the first and not the last developer who needs to read a file, no need to reinvent it each time.

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

回答by Dean J

Unless you really need to do it just that way, maybe simplify what you're doing.

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

Doing everythingin the for loop may seem like a very slick way of doing it, but it's shooting yourself in the foot when you need to debug and don't immediately see the solution.

在 for 循环中做所有事情似乎是一种非常巧妙的方式,但是当您需要调试并且没有立即看到解决方案时,这会让您感到沮丧。