java打开float文件_如何將Java FloatBuffer(或任何緩沖區)保存到文件中

UPDATE FOR BINARY OUTPUT:

二進制輸出的更新:

// There are dependencies on how you create your floatbuffer for this to work

// I suggest starting with a byte buffer and using asFloatBuffer() when

// you need it as floats.

// ByteBuffer b = ByteBuffer.allocate(somesize);

// FloatBuffer fb = b.asFloatBuffer();

// There will also be endiance issues when you write binary since

// java is big-endian. You can adjust this with Buffer.order(...)

// b.order(ByteOrder.LITTLE_ENDIAN)

// If you're using a hex-editor you'll probably want little endian output

// since most consumer machines (unless you've got a sparc / old mac) are little

FileOutputStream fos = new FileOutputStream("some_binary_output_file_name");

FileChannel channel = fos.getChannel();

channel.write(byteBufferBackingYourFloatBuffer);

fos.close();

TEXT OUTPUT: Since you want this to be viewable I assume you want a text file. You'll want to use a PrintStream.

TEXT OUTPUT:由於您希望這是可見的,我假設您需要一個文本文件。你會想要使用PrintStream。

// Try-catch omitted for simplicity

PrintStream ps = new PrintStream("some_output_file.txt");

for(int i = 0; i < yourFloatBuffer.capacity(); i++)

{

// put each float on one line

// use printf to get fancy (decimal places, etc)

ps.println(yourFloagBuffer.get(i));

}

ps.close();

Didn't have time to post a full raw/binary (non-text) version of this. If you want to do that use a FileOutputStream, get the FileChannel, and directly write the FloatBuffer (since it's a ByteBuffer)

沒有時間發布完整的原始/二進制(非文本)版本。如果你想這樣做,請使用FileOutputStream,獲取FileChannel,並直接編寫FloatBuffer(因為它是一個ByteBuffer)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值