自定义 BufferReader 读取并返回字符串直到满足模式
java BufferReader
有一个方法 readLine
,它会读取直到 '\n'
或 '\r'
被读取,然后返回行字符串。示例:
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
BufferReader br =new BufferReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
// Do something
}
我想要一个自定义缓冲区,其读取方式与 BufferReader
完全相同,但在匹配 '\n'
时不返回行,而是应该返回字符串,直到读取模式 <代码>10=???\u0001。
它基本上是一条修复消息,始终以 10=???\u0001
结尾,其中 ?
是数字,\u0001
是字符。
java BufferReader
has a method readLine
which reads till '\n'
or '\r'
is read and then returns the line string. Example:
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
BufferReader br =new BufferReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
// Do something
}
I want a custom buffer which reads exactly like BufferReader
but instead of returning line when it matches '\n'
, it should return the string till it reads a pattern 10=???\u0001
.
It is basically a fix message which always ends with 10=???\u0001
, where ?
is number and \u0001
is a character.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能有兴趣使用
扫描仪
带有自定义分隔符(使用
useDelimiter
)。您确实不应该重写名为
readLine
的方法来执行除读取行之外的任何操作(由\r\n
、\n\r
或\n
,具体取决于您的操作系统)。这是违反直觉的,并且可能会让你自己或其他人在以后感到困惑。You might be interested in using a
Scanner
with a custom delimiter (set usinguseDelimiter
).You really shouldn't override a method named
readLine
to do anything other than read lines (as delimited by\r\n
,\n\r
or\n
based on your operating system). It's counter intuitive and can be confusing to yourself or others later down the road.