我需要在我的Android应用程序中读取由4字节整数(小端)组成的二进制文件到一个2D数组中。我的当前解决方案如下:
DataInputStream inp = null;
try {
inp = new DataInputStream(new BufferedInputStream(new FileInputStream(procData), 32768));
}
catch (FileNotFoundException e) {
Log.e(TAG, "File not found");
}
int[][] test_data = new int[SIZE_X][SIZE_Y];
byte[] buffer = new byte[4];
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
for (int i=0; i < SIZE_Y; i++) {
for (int j=0; j < SIZE_X; j++) {
inp.read(buffer);
byteBuffer = ByteBuffer.wrap(buffer);
test_data[j][SIZE_Y - i - 1] = byteBuffer.order(ByteOrder.LITTLE_ENDIAN).getInt();
}
}
对于一个2k*2k的数组来说,这个速度相当慢,需要大约25秒。 我可以在DDMS中看到垃圾收集器正在加班工作,所以这可能是速度变慢的原因之一。
肯定有更有效的方法使用ByteBuffer将文件读入数组中,但我目前还没有看到它。 有任何想法如何加快速度吗?