Monday 15 February 2010

Read a constant number of characters from a BufferedReader over a socket in Java -


I have installed a socket client in Java and are reading in data using BufferedReader I am I know that a line of input from reader.readLine () method stream will be read.

For example:

 However, I have a constant string I would like to read the number of characters, or by the end of the stream.         Text "itemprop =" text "> 

You will need to insert the reading process in a loop ... stringbuilder data = new stringbilder (128); String text = null; While return data (string =); = null {data. Append (text) .append ("\ n"); // if you are interested in new line characters}; string; / Code>

Your recv method should be thrown to IOException , because it is not actually dealing with these error (IMHO). >

Renew

If you can not guarantee that the line will be eliminated from a new line, then you will need to read each value of it. < stringbuilder data = new stringbilder (128); int charsIn = -1; four buffers = new cells [1024]; while ((charsIn = reader.read (buffer)) gt; -1) {data.append (Buffer, 0, charsIn);} Return data;

Now, the problem is that the stream will not really end because it is in the context of a socket stream Can not Understand (It's Not Ending

Renew

Update

>

  four buffers = new cells [1024]; // It will be read from buffer starting from 0.01 characters in the // character array, to 1024 characters. // It can read at least 1024 characters if the underlying // stream indicates the stream from return -1 and ends / read / process int charsIn = reader.read (buffer, 0, 1024); StringBilder Data = New StringBuilder (character); Data.append (buffer, 0, characters);    

No comments:

Post a Comment