如何将 Java FloatBuffer(或任何 Buffer)保存到文件

发布于 2024-07-16 11:09:10 字数 77 浏览 4 评论 0原文

我有一个已知大小的 FloatBuffer,只想将数据转储到文件(二进制)以便在我的应用程序外部进行检查。 做到这一点最简单的方法是什么?

I have a FloatBuffer of known size and just want to dump the data to a file (in binary) for inspection outside my app. What's the easiest way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

歌枕肩 2024-07-23 11:09:10

二进制输出更新:

// 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)

UPDATE FOR BINARY OUTPUT:

// 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();

TEXT OUTPUT:
Since you want this to be viewable I assume you want a text file. You'll want to use a 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();

Didn't have time to post a full raw/binary (non-text) version of this. If you want to do that use a FileOutputStream, get the FileChannel, and directly write the FloatBuffer (since it's a ByteBuffer)

泪是无色的血 2024-07-23 11:09:10

假设您希望数据为二进制:

ByteBuffer 开始。 调用 asFloatBuffer 获取您的 FloatBuffer。 当您完成工作后,将 ByteBuffer 保存到 WritableByteChannel 中。

如果您已有 FloatBuffer,则可以使用 put 将其从步骤 2 复制到缓冲区中。

性能较低但更简单的方法是使用Float.floatToIntBit

(显然,请注意字节顺序。)

Asusming you want the data as binary:

Start with a ByteBuffer. Call asFloatBuffer to get your FloatBuffer. When you've finished doing your stuff, save the ByteBuffer out to a WritableByteChannel.

If you already have FloatBuffer it can be copied into the buffer from step 2 with a put.

A low performance but easier way would be to use Float.floatToIntBit.

(Watch for endianess, obviously.)

我爱人 2024-07-23 11:09:10

这会迭代缓冲区支持的数组并输出每个浮点数。 只需将文本文件和 floatBuffer 替换为您自己的参数即可。

PrintStream out = new PrintStream("target.txt");
for(float f : floatBuffer.array()){
  out.println(f);
}
out.close();

This iterates through the array backed by your buffer and outputs each float. Just replace the text file and floatBuffer with your own parameters.

PrintStream out = new PrintStream("target.txt");
for(float f : floatBuffer.array()){
  out.println(f);
}
out.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文