可能重复:
Java中的文件到byte []
我想从文件中读取数据并将其解组为Parcel.在文档中不清楚,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 …Run Code Online (Sandbox Code Playgroud)