英文:
ByteBuffer discards trailing newline
问题
这是我将字符串放入ByteBuffer的方法:
String message = "Hello\n\n";
ByteBuffer bresult = ByteBuffer.allocate(message.getBytes().length);
bresult.put(message.getBytes());
bresult.flip();
当我将ByteBuffer转换为字符串以查看结果时,上述字符串中的 \n\n
被移除。
这是我将ByteBuffer转换为字符串的方式:
System.out.print(new String(bresult.array()));
结果是没有任何换行的 "Hello"。
您可以在下面的日志截图中看到结果:
[![enter image description here][1]][1]
但是,当我在 "Hello" 字符串中添加空格,比如 message = "Hello\n\n ";
结果如下:
[![enter image description here][2]][2]
如您所见,在 "Hello" 字符串下方有一些换行符。
英文:
Here is how I put a string to ByteBuffer
String message="Hello\n\n";
ByteBuffer bresult = ByteBuffer.allocate(message.getBytes().length);
bresult.put(message.getBytes());
bresult.flip();
When I convert bytebuffer to string to see the result \n\n is removed from the above string.
This how I convert ByteBuffer to String
print(new String(bresult.array()));
and the result is Hello without any line breaks.
You can see the result in the below screenshot from my log
[![enter image description here][1]][1]
but when I add spaces to hello string like message="Hello\n\n " the result is the below:
[![enter image description here][2]][2]
as you can see there are some line breaks under hello string.
答案1
得分: 4
我无法重现这个问题。以下是代码部分的翻译:
import java.nio.ByteBuffer;
public class App {
public static void main(String[] args) {
String str = "Hello\n\n";
ByteBuffer buf = ByteBuffer.allocate(str.getBytes().length);
buf.put(str.getBytes());
buf.flip();
String str2 = new String(buf.array());
System.out.println(str.equals(str2));
System.out.println(str2.endsWith("\n\n"));
}
}
给出的输出是:
true
true
这意味着从byte[]
创建的String
与原始的String
具有相同的字符。
一些注意事项:
-
上面使用
ByteBuffer
的方式是一个绕弯路,实际上等同于str2 = new String(str.getBytes())
。我使用ByteBuffer
是因为这是你在问题中使用的方式。 -
要小心使用
String#getBytes()
和String#<init>(byte[])
。它们都使用默认的平台编码,这可能会导致问题。考虑显式指定编码方式。 -
如果我用
System.out.print(str2)
替换测试部分,我会得到以下输出:Hello
这是"Hello"后面跟着两个换行符。如果使用
println
,则会有三个换行符。请注意,换行符通常不会直接可见。
英文:
I cannot reproduce the problem. The following:
import java.nio.ByteBuffer;
public class App {
public static void main(String[] args) {
String str = "Hello\n\n";
ByteBuffer buf = ByteBuffer.allocate(str.getBytes().length);
buf.put(str.getBytes());
buf.flip();
String str2 = new String(buf.array());
System.out.println(str.equals(str2));
System.out.println(str2.endsWith("\n\n"));
}
}
Gives this output:
true
true
Which means the String
created from the byte[]
has all the same characters as the original String
.
Some notes:
-
The use of
ByteBuffer
above is a roundabout way of doingstr2 = new String(str.getBytes())
. I usedByteBuffer
since that's what you used in the question. -
Be careful with
String#getBytes()
andString#<init>(byte[])
. Both use the default platform encoding which may or may not cause problems. Consider specifying the encoding explicitly. -
If I replace the test with
System.out.print(str2)
I get the following output:Hello
That's "Hello" followed by two line breaks. If
println
was used instead then there would be three line breaks. Note that line breaks are not typically directly visible.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论