我的文件中有二进制数据(32 位整数值的列表),我需要将其有效地放入 int 数组中。除了将数据加载到 byte[] 中,然后一次将其转换为 int[] 一个元素之外,我看不到任何方法可以做到这一点。对于我需要加载的数据量来说,这太慢了。在实际手机上完成转换大约需要 3 秒。从文件中读取 byte[] 中的数据几乎是瞬时的。
是否有任何库使用 native 方法从文件中读取 int[] 或将 byte[] 转换为 int[]?
最佳答案
你有没有尝试过这样的事情:
File file = new File("binary.file");
FileInputStream fin = new FileInputStream(file);
BufferedInputStream bin = new BufferedInputStream(fin);
DataInputStream din = new DataInputStream(bin);
int count = (int) (file.length() / 4);
int[] values = new int[count];
for (int i = 0; i < count; i++) {
values[i] = din.readInt();
}
即使在手机上,这也应该相对较快,除非您只是处理一个大文件。
关于java - 高效地将二进制数据读入int[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5751006/