用于零终止字符串的 Java BufferedReader
我需要从 Java 中的 InputStream 读取以零结尾的字符串。
是否有类似于 BufferedReader.readLine() 的方法来读取以零结尾的字符串?
I need to read zero-terminated strings from InputStream in Java.
Is there similar to BufferedReader.readLine() method for reading zero-terminated strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不可以。Java 本身不识别以零结尾的字符串。您必须读取
InputStream
并查找 0 字节。请注意,这并没有解决字符编码的问题。
InputStream
将为您提供字节流,然后您必须通过Reader
编码为字符。如果您有多字节字符编码,那么问题会变得更加复杂。No. Java doesn't recognise a zero-terminated string as such. You'll have to read the
InputStream
and look for a 0 byte.Note that this doesn't address the issue of character-encoding. The
InputStream
will give you the stream of bytes, and you'll then have to encode to characters via aReader
. If you have a multi-byte character encoding then the issue becomes more complex.您还需要了解“零”意味着什么。输入/输出流处理字节,而读取器/写入器处理字符。如果您想匹配零字符,则字节到字符转换编码将发挥作用。
you will also need to understand what is that "zero" means . input/output streams deal with bytes whereas readers/writers deal with characters. if you want to match against a zero character then byte to char conversion encoding will come into play.
您可以创建一种类似于下面的方法。从
InputStream
创建一个BufferedReader
。BufferedReader
通过引用传递,因此它将保留状态。它也可以很容易地存储在实例变量中。....
You could create a method similar to the one below. Create a
BufferedReader
from anInputStream
. TheBufferedReader
is passed by reference so it will retain state. It could easily be stored in an instance variable as well.....