2009-04-09 9 views

答えて

4

UPDATE:

// 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だから)

+0

ありがとう、実際に私はバイナリですべてを書きたいと思いましたが。私はそれについて具体的には申し訳ありません、私は質問を更新します。 – jblocksom

-1

を書きます。テキストファイルとfloatBufferを独自のパラメータに置き換えてください。

あなたはバイナリとしてデータを望んAsusming
PrintStream out = new PrintStream("target.txt"); 
for(float f : floatBuffer.array()){ 
    out.println(f); 
} 
out.close(); 
2

ByteBuffer

スタート。 FloatBufferを入手するにはasFloatBufferに電話してください。あなたの作品を終えたら、ByteBufferWritableByteChannelに保存してください。

すでにFloatBufferがある場合は、手順2のバッファにputでコピーできます。

パフォーマンスは低いが、より簡単な方法は、Float.floatToIntBitを使用することです。

(明らかにエンディアンを調べてください)

関連する問題