既知のサイズの FloatBuffer があり、アプリの外部で検査するためにデータをファイル (バイナリ) にダンプしたいだけです。これを行う最も簡単な方法は何ですか?
4186 次
3 に答える
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であるため) 。
于 2009-04-09T19:44:57.873 に答える
2
データをバイナリとして欲しいと仮定します:
から始めByteBuffer
ます。電話asFloatBuffer
して を入手してくださいFloatBuffer
。作業が完了したら、出力を に保存しByteBuffer
ますWritableByteChannel
。
既に持っている場合はFloatBuffer
、ステップ 2 でput
.
パフォーマンスは低いですが、より簡単な方法は を使用することFloat.floatToIntBit
です。
(明らかに、エンディアンに注意してください。)
于 2009-04-09T19:52:28.497 に答える
-1
これは、バッファに裏打ちされた配列を反復処理し、各 float を出力します。テキスト ファイルと floatBuffer を独自のパラメーターに置き換えるだけです。
PrintStream out = new PrintStream("target.txt");
for(float f : floatBuffer.array()){
out.println(f);
}
out.close();
于 2009-04-09T19:51:12.617 に答える