Java 如何将bin文件读取到字节数组?
我有一个bin文件,需要转换成字节数组。谁能告诉我怎么做 以下是我到目前为止的情况:Java 如何将bin文件读取到字节数组?,java,file-io,bytearray,Java,File Io,Bytearray,我有一个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
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 f=新文件(“notification.bin”);
is=新文件输入流(f);
长长度=f.长度();
/*if(长度>整数最大值){
//文件太大
}*/
//创建字节数组以保存数据
字节[]字节=新字节[(int)长度];
//读入字节
整数偏移=0;
int numRead=0;
而(偏移量=0){
偏移量+=numRead;
}
//确保已读入所有字节
if(偏移量<字节长度){
抛出新IOException(“无法完全读取文件”+f.getName());
}
但它不起作用
Kaddy使用内存映射文件可能会更好。请参见使用内存映射文件可能会更好。请参见尝试使用此
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();
}
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();
}
在for循环中做任何事情似乎都是一种非常巧妙的方法,但是当你需要调试时,却没有立即看到解决方案,这会让你自食其果。除非你真的需要这样做,否则也许可以简化你正在做的事情
在for循环中做任何事情似乎都是一种非常巧妙的方法,但是当您需要调试并且没有立即看到解决方案时,这会给您带来麻烦。在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执行此任务可能有一种更有效的方法。在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可能有一种更有效的方法来完成这项任务。在本文中,我从一个URL阅读
您可以修改它,使InputStream来自文件而不是URL连接
比如:
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在这篇文章中,我是从一个URL读到的
您可以修改它,使InputStream来自文件而不是URL连接
比如:
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试试开源库
IOUtils.toByteArray(inputStream)
您不是第一个也不是最后一个需要读取文件的开发人员,无需每次都重新创建文件。试试开源库
IOUtils.toByteArray(inputStream)
您不是第一个也不是最后一个需要读取文件的开发人员,无需每次都重新创建文件。我为您解决了这个问题。请试着将你所发布的问题格式化,使其智能化和易读。以什么方式不起作用?您使用的文件类是什么?为什么不使用标准的std::ifstream呢?可能是因为它是C#而不是C++这不是C#-我相信它是Java。无论如何,它看起来更像Java而不是C#。我已经相应地编辑了标签。我为您修复了它。请试着将你所发布的问题格式化,使其智能化和易读。以什么方式不起作用?您使用的文件类是什么?为什么不使用标准的std::ifstream呢?可能是因为它是C#而不是C++这不是C#-我相信它是Java。无论如何,它看起来更像Java而不是C#。我已经相应地编辑了标记。那是关于什么的
DataOutputStream
?+1这是一个很好的捕获Tom,ByteArrayOutputStream在这种情况下就足够了,我们只需要读取字节和写入字节。如果我们要另外编写基本类型,可能需要DataOutputStream,DataOutputStream
是关于什么的?+1这是一个很好的陷阱,在这种情况下,ByteArrayOutputStream就足够了,我们只需读取字节和写入字节。如果我们要另外编写基元类型,那么可能需要DataOutputStream