我有一個已知大小的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