Search code examples
javasocketsbufferedreaderjava-io

java bufferedreader, declaration readline


I am implementing a Socket, which it will accept data from a server. The server will send data record, which will be separated by the 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)

ETX = End Of Text (ASCII character)


Solution

  • If you read the Javadoc for BufferedReader.readLine()

    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.

    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.