The question is published on by Tutorial Guruji team.
So I learned recently that the standard basic networking in Java goes like this:
out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
I just want to clarify, so for the output we don’t use a BufferedWriter
because that’s the job on the server’s side? Would it be wrong to do something like this:
out = new BufferedWriter(PrintWriter(echoSocket.getOutputStream(), true));
Answer
From the Java documentation:
http://docs.oracle.com/javase/tutorial/essential/io/buffers.html
Most of the examples we’ve seen so far use unbuffered I/O. This means each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.
To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.
Yes, “buffered” input AND output is usually a Good Idea.
IMHO…
PS:
I don’t see anything wrong with PrintWriter. Especially if I wanted to do “printf()” style text I/O directly to the socket.