我正在尝试使用java代码在android设备中编写和读取float数组(实际上相当大,640 * 480)。 比如this one
DataOutputStream out = ...;
for (int i=0; i<floatarray.length; ++i)
out.writeFloat(floatarray[i]);
很慢,我已经尝试过了。
WRITE:
float[] test=new float[3];
test[0]=1.0f;
test[1]=1.2f;
test[2]=1.5f;
//long timeBeforeWrite = System.nanoTime();
try {
BufferedOutputStream dataOut = new BufferedOutputStream (
new FileOutputStream("/sdcard/DCIM/Camera/Dual/demo.bin"));
byte buf[]=new byte[4*test.length];
long timeBeforeWrite = System.nanoTime();
for (int i=0; i<test.length; ++i)
{
int val = Float.floatToRawIntBits(test[i]);
buf[4 * i] = (byte) (val >> 24);
buf[4 * i + 1] = (byte) (val >> 16) ;
buf[4 * i + 2] = (byte) (val >> 8);
buf[4 * i + 3] = (byte) (val);
}
dataOut.write(buf);
long ct_write = System.nanoTime();
long offset_write = ct_write - timeBeforeWrite;
float mOffsetInMs_write = (float)(offset_write)/1000000;
Log.e("ETA", "write float[] " + Float.toString(mOffsetInMs_write));
dataOut.flush();
dataOut.close();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
READ:
float[] read=new float[3];
try{
BufferedInputStream dataIn=new BufferedInputStream (new FileInputStream("/sdcard/DCIM/Camera/Dual/demo.txt"));
byte buf[]=new byte[4*read.length];
long timeBeforeWrite = System.nanoTime();
dataIn.read(buf);
for (int i=0; i<read.length; ++i)
{
int val;
val = buf[4 * i] << 24;
val += buf[4 * i + 1] << 16;
val += buf[4 * i + 2] << 8;
val += buf[4 * i + 3];
read[i]=Float.valueOf(Integer.toBinaryString(val));
//int val = Float.floatToRawIntBits(disparityMap[i]);
}
long ct_write = System.nanoTime();
long offset_write = ct_write - timeBeforeWrite;
float mOffsetInMs_write = (float)(offset_write)/1000000;
Log.e("ETA", "read float[] " + Float.toString(mOffsetInMs_write));
dataIn.close();
}catch (IOException ex) {
System.err.println(ex.getMessage());
}
回读的东西是非常奇怪的浮点值,这有什么问题?顺便说一下,回读运行得很慢,不知道为什么。
或者快速读/写浮点数组的任何其他好主意?
谢谢!
//感谢Larry,我尝试了一下bytebuffer.asfloatbuffer()方式:
//WRITE
float[] test = new float[3];
test[0]=1.1f;
test[1]=1.2f;
test[2]=1.5f;
try{
RandomAccessFile aFile = new RandomAccessFile("/sdcard/demo.data", "rw");
FileChannel outChannel = aFile.getChannel();
//one float 3 bytes
ByteBuffer buf = ByteBuffer.allocate(12);
buf.clear();
buf.asFloatBuffer().put(test);
//while(buf.hasRemaining())
{
outChannel.write(buf);
}
outChannel.close();
}
catch (IOException ex) {
System.err.println(ex.getMessage());
}
//READ
float[] readback=new float[3];
try{
RandomAccessFile rFile = new RandomAccessFile("/sdcard/demo.data", "rw");
FileChannel inChannel = rFile.getChannel();
ByteBuffer buf_in = ByteBuffer.allocate(12);
buf_in.clear();
inChannel.read(buf_in);
buf_in.asFloatBuffer().get(readback);
inChannel.close();
}
catch (IOException ex) {
System.err.println(ex.getMessage());
}
并且程序在buf_in.asFloatBuffer().get(readback);
中崩溃
任何想法,有没有方法进入和调试内部java,抱歉全新的java世界。再次感谢
答案 0 :(得分:7)
感谢Larry,我只是在分享我的代码,希望它可以帮助其他人。
我只想弄清楚,我们需要在读/写文件通道后回放到缓冲区。以下代码正常运行,运行速度非常快。
float[] disparity=new float[640*480];
disparity[1]=1.5f;
disparity[2]=4.566f;
//WRITE
try{
RandomAccessFile aFile = new RandomAccessFile("/sdcard/demo.data", "rw");
FileChannel outChannel = aFile.getChannel();
//one float 4 bytes
ByteBuffer buf = ByteBuffer.allocate(4*640*480);
buf.clear();
buf.asFloatBuffer().put(disparity);
//while(buf.hasRemaining())
{
outChannel.write(buf);
}
//outChannel.close();
buf.rewind();
float[] out=new float[3];
buf.asFloatBuffer().get(out);
outChannel.close();
}
catch (IOException ex) {
System.err.println(ex.getMessage());
}
//READ
float[] readback=new float[640*480];
try{
RandomAccessFile rFile = new RandomAccessFile("/sdcard/demo.data", "rw");
FileChannel inChannel = rFile.getChannel();
ByteBuffer buf_in = ByteBuffer.allocate(640*480*4);
buf_in.clear();
inChannel.read(buf_in);
buf_in.rewind();
buf_in.asFloatBuffer().get(readback);
inChannel.close();
}
catch (IOException ex) {
System.err.println(ex.getMessage());
}
再次感谢。
答案 1 :(得分:6)
尝试使用FileChannel
进行文件访问,使用ByteBuffer
进行数据访问。您可以使用ByteBuffer
方法将数据放入ByteBuffer.putFloat()
,并使用FileChannel.write()
将其写出来。当您回读它时,您可以调用FileChannel.read()
来获取表示数据的ByteBuffer,然后调用ByteBuffer.asFloatBuffer()
以获得表示数据的FloatBuffer
,然后可以使用该float[]
来获取{{1}} 1}}。
答案 2 :(得分:0)
只需在DataOutputStream和FileOutputStream之间添加一个BufferedOutputStream。