The Psicopath
The Psicopath

Reputation: 75

BufferedReader, other Object to get a String

In java there is another Object like BufferedReader to read data recived by server?? Because the server send a string without newline and the client don't print any string untile the Server close the connection form Timeout (the timeout message have a newline!) , after the Client print all message recived and the timeout message send by server! help me thanks!!

Upvotes: 1

Views: 1561

Answers (4)

Peter Lawrey
Peter Lawrey

Reputation: 533432

You could try

InputStream is = ... // from input
String text = IOUtils.toString(is);

turns the input into text, without without newlines (it preserves original newlines as well)

Upvotes: 0

whaley
whaley

Reputation: 16265

You asked for another class to use, so in that case give Scanner a try for this. It's usually used for delimiting input based on patterns or by the types inferred from the input (e.g. reading on a byte-by-byte bases or an int-by-int basis, or some combination thereof). However, you can use it as just a general purpose "reader" here as well, to cover your use case.

Upvotes: 2

BalusC
BalusC

Reputation: 1108537

Just don't read by newlines using readLine() method, but read char-by-char using read() method.

for (int c = 0; (c = reader.read()) > -1;) {
    System.out.print((char) c);
}

Upvotes: 2

Roland Illig
Roland Illig

Reputation: 41617

When you read anything from a server, you have to strictly follow the communication protocol. For example the server might be an HTTP server or an SMTP server, it may encrypt the data before sending it, some of the data may be encoded differently, and so on.

So you should basically ask: What kind of server do I want to access? How does it send the bytes to me? And has someone else already done the work of interpreting the bytes so that I can quickly get to the data that I really want?

If it is an HTTP server, you can use the code new URL("http://example.org/").openStream(). Then you will get a stream of bytes. How you convert these bytes into characters, strings and other stuff is another task.

Upvotes: 1

Related Questions