CharBuffer.put() 不起作用

发布于 2024-12-28 10:38:44 字数 410 浏览 3 评论 0原文

我尝试使用 CharBuffer.put() 函数将一些字符串放入 CharBuffer 中 但缓冲区留空。

我的代码:

CharBuffer charBuf = CharBuffer.allocate(1000);
for (int i = 0; i < 10; i++) 
{
    String text = "testing" + i + "\n";
    charBuf.put(text);
}
System.out.println(charBuf);

我尝试在 allocate(1000) 之后与 clear()rewind() 一起使用,但这并没有改变结果。

I try to put some strings to CharBuffer with CharBuffer.put() function
but the buffer is left blank.

my code:

CharBuffer charBuf = CharBuffer.allocate(1000);
for (int i = 0; i < 10; i++) 
{
    String text = "testing" + i + "\n";
    charBuf.put(text);
}
System.out.println(charBuf);

I tried to use with clear() or rewind() after allocate(1000) but that did not change the result.

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

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

发布评论

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

评论(4

掐死时间 2025-01-04 10:38:44

试试这个:

CharBuffer charBuf = CharBuffer.allocate(1000);
for (int i = 0; i < 10; i++) 
{
    String text = "testing" + i + "\n";
    charBuf.put(text);
}
charBuf.rewind();
System.out.println(charBuf);

您缺少的细节是写入将当前指针移动到写入数据的末尾,因此当您打印出来时,它从当前指针开始,而当前指针没有写入任何内容。

Try this:

CharBuffer charBuf = CharBuffer.allocate(1000);
for (int i = 0; i < 10; i++) 
{
    String text = "testing" + i + "\n";
    charBuf.put(text);
}
charBuf.rewind();
System.out.println(charBuf);

The detail you're missing is that writing moves the current pointer to the end of the written data, so when you're printing it out, it's starting at the current pointer, which has nothing written.

小清晰的声音 2025-01-04 10:38:44

添加对 < 的调用code>rewind() 就在循环之后

Add a call to rewind() right after the loop.

梦回旧景 2025-01-04 10:38:44

您需要先flip() 缓冲区,然后才能从缓冲区中读取数据。从缓冲区读取数据之前需要调用flip()方法。当调用flip() 方法时,limit 被设置为当前位置,position 被设置为0。例如,

CharBuffer charBuf = CharBuffer.allocate(1000);
for (int i = 0; i < 10; i++) 
{
    String text = "testing" + i + "\n";
    charBuf.put(text);
}
charBuf.flip();
System.out.println(charBuf);

上面的代码只会打印缓冲区中的字符,而不打印缓冲区中未写入的空间。

You will need to flip() the buffer before you can read from the buffer. The flip() method needs to be called before reading the data from the buffer. When the flip() method is called the limit is set to the current position, and the position to 0. e.g.

CharBuffer charBuf = CharBuffer.allocate(1000);
for (int i = 0; i < 10; i++) 
{
    String text = "testing" + i + "\n";
    charBuf.put(text);
}
charBuf.flip();
System.out.println(charBuf);

The above will only print the characters in the buffers and not the unwritten space in the buffer.

带刺的爱情 2025-01-04 10:38:44

放入物品后必须倒带,试试这个

CharBuffer charBuf = CharBuffer.allocate(1000);
for (int i = 0; i < 10; i++) 
{
    String text = "testing" + i + "\n";
    charBuf.put(text);
}
charBuf.rewind();
System.out.println(charBuf);

You must rewind it after you put in the objects, try this

CharBuffer charBuf = CharBuffer.allocate(1000);
for (int i = 0; i < 10; i++) 
{
    String text = "testing" + i + "\n";
    charBuf.put(text);
}
charBuf.rewind();
System.out.println(charBuf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文