java快速读写浮点数组

java快速读写浮点数组,java,android,arrays,io,floating-point,Java,Android,Arrays,Io,Floating Point,我正在尝试用java代码在android设备中编写和读取浮点数组(实际上相当大,640*480)。 像 程序在buf_in.asFloatBuffer().get(readback)中崩溃 任何想法和有没有好的方法可以在java中进行内部调试,抱歉,这在java世界是全新的。再次感谢尝试使用FileChannel访问文件,使用ByteBuffer访问数据。您可以使用ByteBuffer.putFloat()方法将数据放入ByteBuffer中,并使用FileChannel.write()将其写出

我正在尝试用java代码在android设备中编写和读取浮点数组(实际上相当大,640*480)。 像

程序在
buf_in.asFloatBuffer().get(readback)中崩溃

任何想法和有没有好的方法可以在java中进行内部调试,抱歉,这在java世界是全新的。再次感谢尝试使用
FileChannel
访问文件,使用
ByteBuffer
访问数据。您可以使用
ByteBuffer.putFloat()
方法将数据放入
ByteBuffer
中,并使用
FileChannel.write()
将其写出。回读时,可以调用
FileChannel.read()
获取表示数据的ByteBuffer,然后调用
ByteBuffer.asFloatBuffer()
获取表示数据的
FloatBuffer
,然后可以使用该数据获取
float[]

只需在DataOutputStream和FileOutputStream之间添加一个BufferedOutputStream。

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

再次感谢。

非常感谢,我已经更新了我的问题,readback在buf_in.asFloatBuffer().get(readback)中不起作用;对此有什么想法吗?您必须在
finally
块中关闭文件,或者使用
try with resources
!在读取期间倒带是正确的,但在写入期间倒带不是正确的。我建议你这样做:
try(ByteChannel channel=Files.newByteChannel(path,WRITE,CREATE,TRUNCATE_EXISTING)){ByteBuffer buf=ByteBuffer.allocate(disparence.length*4);buf.asFloatBuffer().put(disparence);channel.WRITE(buf);}
我和你有同样的问题。。。“buf_in.asFloatBuffer().get(readback);”崩溃。。。问题解决了吗?这里缺少的问题是缓冲器的位置错误。您需要调用它的位置(0)。
            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());
        }
//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());
    }
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());
    }