2009-04-09 53 views

回答

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)通过你的缓冲区的支持数组

+0

谢谢,尽管我其实想这一切写出来的二进制文件。对不起,没有具体说明,我会更新问题。 – jblocksom 2009-04-09 19:50:45

-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

(留意字节序,很明显。)

相关问题