我有一个大小为500MB的二进制文件,里面包含许多浮点数数据。这个文件是由一个C++程序编写而来的。在C++中,我可以通过以下方式载入它:
void load(char f_name[], int size, float data[])
{
std::fstream f_bin(f_name, std::ios::in|std::ios::binary);
f_bin.seekg(std::ios::beg);
f_bin.read((char*)data, size*sizeof(float));
f_bin.close();
}
float *data;
int size = 123456789;
data = new float[size];
load("myFile.bin", size, data);
我可以访问浮点值:data[x];在C++中它工作得相当快。Java中是否有类似的东西?
编辑 阅读了一些资料之后,我目前有以下进展:
RandomAccessFile f = new RandomAccessFile("C://path//myFile.bin", "r");
byte[] bytes = new byte[(int)f.length()];
f.read(bytes);
float fl = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat();
System.out.println(fl);
这段代码输出了第一个浮点数。现在我需要逐个遍历浮点数,并将其放入像float[] data
这样的数组中。