Java中将文件读入byte[]数组的优雅方法
可能的重复:
Java 中的文件到 byte[]
我想从文件中读取数据并将其解组到包裹。 在文档中尚不清楚 FileInputStream 有方法读取其所有内容。为了实现这一点,我执行以下操作:
FileInputStream filein = context.openFileInput(FILENAME);
int read = 0;
int offset = 0;
int chunk_size = 1024;
int total_size = 0;
ArrayList<byte[]> chunks = new ArrayList<byte[]>();
chunks.add(new byte[chunk_size]);
//first I read data from file chunk by chunk
while ( (read = filein.read(chunks.get(chunks.size()-1), offset, buffer_size)) != -1) {
total_size+=read;
if (read == buffer_size) {
chunks.add(new byte[buffer_size]);
}
}
int index = 0;
// then I create big buffer
byte[] rawdata = new byte[total_size];
// then I copy data from every chunk in this buffer
for (byte [] chunk: chunks) {
for (byte bt : chunk) {
index += 0;
rawdata[index] = bt;
if (index >= total_size) break;
}
if (index>= total_size) break;
}
// and clear chunks array
chunks.clear();
// finally I can unmarshall this data to Parcel
Parcel parcel = Parcel.obtain();
parcel.unmarshall(rawdata,0,rawdata.length);
我认为这段代码看起来很难看,我的问题是: 如何漂亮地将文件中的数据读取到byte[]中? :)
Possible Duplicate:
File to byte[] in Java
I want to read data from file and unmarshal it to Parcel.
In documentation it is not clear, that FileInputStream has method to read all its content. To implement this, I do folowing:
FileInputStream filein = context.openFileInput(FILENAME);
int read = 0;
int offset = 0;
int chunk_size = 1024;
int total_size = 0;
ArrayList<byte[]> chunks = new ArrayList<byte[]>();
chunks.add(new byte[chunk_size]);
//first I read data from file chunk by chunk
while ( (read = filein.read(chunks.get(chunks.size()-1), offset, buffer_size)) != -1) {
total_size+=read;
if (read == buffer_size) {
chunks.add(new byte[buffer_size]);
}
}
int index = 0;
// then I create big buffer
byte[] rawdata = new byte[total_size];
// then I copy data from every chunk in this buffer
for (byte [] chunk: chunks) {
for (byte bt : chunk) {
index += 0;
rawdata[index] = bt;
if (index >= total_size) break;
}
if (index>= total_size) break;
}
// and clear chunks array
chunks.clear();
// finally I can unmarshall this data to Parcel
Parcel parcel = Parcel.obtain();
parcel.unmarshall(rawdata,0,rawdata.length);
I think this code looks ugly, and my question is:
How to do read data from file into byte[] beautifully? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
很久以前:
调用其中任何一个
从
http://commons.apache.org/io/
如果库占用空间对于您的 Android 应用程序来说太大,您现在可以使用 commons-io 库中的相关类
(Java 7+ 或 Android API 级别 26+)
幸运的是,我们现在在 nio 包中提供了一些方便的方法。例如:
这里是 Javadoc
A long time ago:
Call any of these
From
http://commons.apache.org/io/
If the library footprint is too big for your Android app, you can just use relevant classes from the commons-io library
Today (Java 7+ or Android API Level 26+)
Luckily, we now have a couple of convenience methods in the nio packages. For instance:
Javadoc here
这也将起作用:
This will also work:
如果您使用 Google Guava(如果没有,您应该使用),您可以调用:< a href="http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/io/ByteStreams.html#toByteArray(java.io.InputStream)" rel="noreferrer">< code>ByteStreams.toByteArray(InputStream) 或
Files.toByteArray(File)
If you use Google Guava (and if you don't, you should), you can call:
ByteStreams.toByteArray(InputStream)
orFiles.toByteArray(File)
这对我有用:
This works for me:
使用
ByteArrayOutputStream
。流程如下:InputStream
来读取数据ByteArrayOutputStream
。InputStream
复制到OutputStream
toByteArray()
方法Use a
ByteArrayOutputStream
. Here is the process:InputStream
to read dataByteArrayOutputStream
.InputStream
into theOutputStream
byte[]
from theByteArrayOutputStream
using thetoByteArray()
method看看下面的 apache commons 函数:
Have a look at the following apache commons function: