java bufferedreader,声明readline

java bufferedreader, declaration readline

我正在实现一个套接字,它将接受来自服务器的数据。服务器会发送数据记录,数据记录由stx,data,etx分隔。我的问题是,如果我使用以下代码:

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
 }

它会自动将每条数据记录分开?还是我必须自己做?谢谢

STX = 文本开始(ASCII 字符)

ETX = 文本结束(ASCII 字符)

如果您阅读 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.

所以你可以看到它并没有像你想要的那样做任何事情。

Or I have to do it by myself?

我建议您要么使用带有自定义分隔符的 Scanner,要么一次解析 BufferedReader 一个字符。