user1424139
user1424139

Reputation: 11

BufferedWriter vs PrintWriter with sockets?

In this case, optimize perfomance in sockets java, what is better, with sockets, BufferedWriter or PrintWriter?

The socket in every thread (client) send the formatted data in lines, string with CR. In BufferedWriter, i would use newLine after write()?

Thanks for your help.

Regards.

Upvotes: 0

Views: 2237

Answers (2)

user207421
user207421

Reputation: 311048

PrintWriter swallows all exceptions. Unless you use great care to check for errors every time you call it, and are prepared not to know what those errors actually were, it shouldn't normally be used over a network, where you need to know about all exceptions. Use BufferedWriter.

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198471

The Javadoc of BufferedWriter specifically states:

Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,

 PrintWriter out
   = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));

Upvotes: 1

Related Questions