java - 以java快速方式写入/读取 float 组

标签 java android arrays io floating-point

我正在尝试使用java代码在android设备中写入和读取 float 组(实际上相当大,640*480)。 像this one

    DataOutputStream out = ...;
for (int i=0; i<floatarray.length; ++i)
    out.writeFloat(floatarray[i]);

非常慢,我已经尝试过了。

写:

            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());
        }

阅读:

            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());
        }

读回的东西是非常奇怪的浮点值,这有什么问题?顺便说一句,读回运行得很慢,不知道为什么。 或者还有其他快速读/写 float 组的好主意吗?

谢谢!

//感谢 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世界中是全新的。再次感谢

最佳答案

感谢 Larry,我只是分享我的代码,希望对其他人有帮助。

我刚刚弄清楚,我们需要在读/写文件 channel 后倒回缓冲区。以下代码可以正常工作,并且运行速度相当快。

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());
    }

再次感谢。

关于java - 以java快速方式写入/读取 float 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22249483/

相关文章:

java - 找不到接口(interface) org.springframework.data.domain.Pageable 的主构造函数或默认构造函数

Android 应用程序,无需市场即可更新。未安装应用

javascript - 通过替换值来改变或创建新数组

c++ - 为什么此代码片段会返回意外结果?

java - 用于返回用户输入字符串中的元音的 Junit 测试?

java - 如何将 CSV 文件解码为 java 对象

java - 我可以将 WindowListener 添加到 MenuItem 吗?

android - Android 中的按钮和背景大小/分辨率

android - 如何在不重新安装应用程序的情况下调试它?

javascript - 我想从 php 例程中删除一些 if 循环,使代码更加紧凑和动态