简体   繁体   English

带有套接字的BufferedWriter与PrintWriter?

[英]BufferedWriter vs PrintWriter with sockets?

In this case, optimize perfomance in sockets java , what is better, with sockets, BufferedWriter or PrintWriter? 在这种情况下,使用套接字,BufferedWriter或PrintWriter 优化套接字java中的性能 ,哪个更好?

The socket in every thread (client) send the formatted data in lines, string with CR. 每个线程(客户端)中的套接字以行的形式发送格式化的数据,字符串为CR。 In BufferedWriter, i would use newLine after write()? 在BufferedWriter中,我会在write()之后使用newLine吗?

Thanks for your help. 谢谢你的帮助。

Regards. 问候。

The Javadoc of BufferedWriter specifically states: BufferedWriterJavadoc特别声明:

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. 除非需要及时输出,否则建议将BufferedWriter包装在其write()操作可能会很昂贵的所有Writer周围,例如FileWriters和OutputStreamWriters。 For example, 例如,

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

They makes absolutely different things. 他们做出了完全不同的事情。 One reads from stream and the other writes to stream. 一个从流读取,另一个从流写入。 How can you compare their performance? 您如何比较他们的表现?

PrintWriter swallows all exceptions. PrintWriter吞噬所有异常。 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. 使用BufferedWriter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM