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

时间:2021-03-27 13:33:09

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

6 个解决方案

#1


2  

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答案

#2


1  

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

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

#3


1  

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可能有更有效的方法来完成此任务。

#4


0  

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

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

Doing everything in 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循环中执行所有操作可能看起来像是一种非常光滑的方式,但是当您需要调试并且不立即看到解决方案时,它会在脚下拍摄。

#5


0  

In this answer I read from an URL

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

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

您可以修改它,以便InputStream来自File而不是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

#6


0  

Try open source library apache commons-io IOUtils.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)您不是第一个而不是最后一个需要读取文件的开发人员,不需要每次都重新发送它。

#1


2  

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答案

#2


1  

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

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

#3


1  

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可能有更有效的方法来完成此任务。

#4


0  

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

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

Doing everything in 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循环中执行所有操作可能看起来像是一种非常光滑的方式,但是当您需要调试并且不立即看到解决方案时,它会在脚下拍摄。

#5


0  

In this answer I read from an URL

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

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

您可以修改它,以便InputStream来自File而不是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

#6


0  

Try open source library apache commons-io IOUtils.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)您不是第一个而不是最后一个需要读取文件的开发人员,不需要每次都重新发送它。