相关疑难解决方法(0)

在Java中将文件读入byte []数组的优雅方式

可能重复:
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)

java file-io android file

70
推荐指数
6
解决办法
16万
查看次数

标签 统计

android ×1

file ×1

file-io ×1

java ×1