简体   繁体   English

java bufferedreader,声明readline

[英]java bufferedreader, declaration readline

I am implementing a Socket, which it will accept data from a server. 我正在实现一个Socket,它将接受来自服务器的数据。 The server will send data record, which will be separated by the stx,data,etx. 服务器将发送数据记录,该记录将由stx,data,etx分隔。 My question is, if I use the following code: 我的问题是,如果我使用以下代码:

client = new Socket("XXX.XXX.XXX.XXX", 50000);
BufferedReader d = new BufferedReader(new InputStreamReader(client.getInputStream()));

 String line;
 while ((line = d.readLine()) != null) {
     // do something
 }

It will automatically do the seperation of each data record? 会自动将每个数据记录分开吗? Or I have to do it by myself? 还是我必须自己做? Thanks 谢谢

STX = Start Of Text (ASCII character) STX =文本开始(ASCII字符)

ETX = End Of Text (ASCII character) ETX =文本结尾(ASCII字符)

If you read the Javadoc for BufferedReader.readLine() 如果您阅读BufferedReader.readLine()的Javadoc

Reads a line of text. 读取一行文本。 A line is considered to be terminated by any one of a line feed ('\\n'), a carriage return ('\\r'), or a carriage return followed immediately by a linefeed. 一行被认为由换行符('\\ n'),回车符('\\ r')或回车符后立即换行符中的任何一个终止。

So you can see it doesn't do anything like what you want. 因此,您可以看到它并没有执行您想要的任何操作。

Or I have to do it by myself? 还是我必须自己做?

I suggest you either use Scanner with a custom delimiter or you parse the BufferedReader one character at a time. 我建议您将Scanner与自定义定界符一起使用,或者一次将BufferedReader解析为一个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM