我有一个已知大小的FloatBuffer,只是想将数据转储到文件(二进制文件)以供我的应用程序之外的检查。最简单的方法是什么?如何将Java FloatBuffer(或任何缓冲区)保存到文件
2
A
回答
4
更新二进制输出:
// 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();
文本输出: 既然你想这是我观看假设你想要的文本文件。你会想要使用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();
没有时间发布完整的原始/二进制(非文本)版本。如果你想这样做,使用FileOutputStream,得到FileChannel,并直接写FloatBuffer(因为它是一个ByteBuffer)通过你的缓冲区的支持数组
-1
这种迭代并输出每个浮动。用你自己的参数替换文本文件和floatBuffer。
PrintStream out = new PrintStream("target.txt");
for(float f : floatBuffer.array()){
out.println(f);
}
out.close();
2
Asusming你想要的数据为二进制:
开始用ByteBuffer
。拨打asFloatBuffer
即可获得您的FloatBuffer
。当你完成你的东西时,将ByteBuffer
保存到WritableByteChannel
。
如果你已经有FloatBuffer
它可以从复制到步骤2的缓冲区。
低性能但更简单的方法是使用Float.floatToIntBit
。
(留意字节序,很明显。)
相关问题
- 1. XML任务,如何保存缓冲区?
- 2. 如何在Emacs中保存所有文件(或保存所有缓冲区)?
- 3. 将Gtk#文本缓冲区内容保存到文件
- 4. 将图像保存到缓冲区
- 5. C# - 如何将IntPtr缓冲区数据保存到文件(最快的方式)?
- 6. 如何显示已保存为缓冲区的文件?
- 7. 将文件加载/流式传输到缓冲区/缓冲区
- 8. 如何将stderr重定向到没有任何缓冲区的文件?
- 9. 如何将内存写入文件缓冲区进行变异?
- 10. 将缓冲区写入文件 - 如何保存二进制格式?
- 11. 如何将GLSurfaceView保存为纹理/缓冲区/位图
- 12. 如何保证我的文件尾随Linux缓存中的最新缓冲区
- 13. WriteFileGather - 将缓冲区追加到文件
- 14. 将32位RGBA缓冲区保存为.png文件(Cocoa OSX)
- 15. 如何将变量保存到工作区或文件中?
- 16. 如何禁用PHPStorm保存到缓冲区?
- 17. HDF5:“文件缓冲区”和“文件缓存”有什么区别?
- 18. 如何强制ECB将GUD缓冲区视为comilation缓冲区?
- 19. 如何将BGRA缓冲区转换为RGBA缓冲区格式?
- 20. 如何将缓冲区写入缓冲区
- 21. 保存流或缓冲的音频文件,文档文件夹
- 22. 如何将文件加载到缓冲区并在Emacs启动时切换到缓冲区
- 23. 如何更改我的暂存缓冲区的保存位置?
- 24. 协议缓冲区文件?
- 25. C:读取二进制文件到内存,更改缓冲区,写入缓冲区到文件
- 26. 如何将log4cplus appender输出发送到内存缓冲区?
- 27. 如何将原始内存复制到nodejs中的缓冲区?
- 28. 如何从文件夹加载文件到内存流缓冲区
- 29. 如何将图像或文件保存到保存箱中xamarin?
- 30. 如何在scala中将新缓冲区合并到旧缓冲区中?
谢谢,尽管我其实想这一切写出来的二进制文件。对不起,没有具体说明,我会更新问题。 – jblocksom 2009-04-09 19:50:45