繁体   English   中英

如何使用filechannel和bytebuffer将double写入文件?

[英]How to write double into file, using filechannel and bytebuffer?

我尝试以下方法,但似乎有问题。

    File f=new File("D:\\aa.txt");
    FileOutputStream fos =new FileOutputStream(f);
    FileChannel fc=fos.getChannel();
    ByteBuffer buf=ByteBuffer.allocate(128);
    for (int i=0;i<16;i++) {
        buf.putDouble(i*8,Math.sqrt(i));            
    }
    fc.write(buf);
    fc.close();

您需要在写入缓冲区之前先flip()缓冲区,然后在循环时再compact()缓冲区。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM