简体   繁体   English

Java客户端/服务器 - 使用BufferedWriter而不是PrintWriter

[英]Java Client/Server - Using BufferedWriter instead of PrintWriter

In all examples of a Java client/server, I've seen BufferedReader used for receiving data, like in 在Java客户端/服务器的所有示例中,我已经看到用于接收数据的BufferedReader ,例如

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

and PrintWriter for sending data, like in PrintWriter一起发送数据,比如

PrintWriter writer = new PrintWriter(socket.getOutputStream());

But can't I just use a BufferedWriter instead of a PrintWriter ? 但我不能只使用BufferedWriter而不是PrintWriter吗? I only need to send unformatted String betweens client and server, so a BufferedWriter should give better performance (not that this is a problem). 我只需要在客户端和服务器之间发送未格式化的字符串,因此BufferedWriter应该提供更好的性能(而不是这是一个问题)。

PrintWriter essentially provides convenience methods around a Writer. PrintWriter本质上提供了围绕Writer的便捷方法。 If you don't need those convenience method-- but just need to write chars-- then functionally, you can use any flavour of Writer you choose, including a 'raw' OutputStreamWriter. 如果你不需要那些方便的方法 - 但只需要编写字符 - 那么在功能上,你可以使用你选择的任何类型的Writer,包括'raw'OutputStreamWriter。

If you are writing chars one at a time, and your socket stream isn't buffered, then it would be advisable to put some buffering in somewhere, either by using a BufferedWriter or by wrapping a BufferedOuputStream around your raw output stream. 如果您一次只编写一个字符,并且您的套接字流没有缓冲,那么建议在某处使用BufferedWriter或在原始输出流周围包装BufferedOuputStream进行缓冲。 An example of where you don't typically need to do this is in a servlet, where the streams passed to your servlet are typically already buffered. 通常不需要执行此操作的示例是在servlet中,传递给servlet的流通常已经缓冲。

PrintWriter also has the "feature" of swallowing exceptions on write methods, which you have to then explicitly check for with checkError() [hands up who actually does this, and who just assumes that the write succeeded...]. PrintWriter还具有在写入方法上吞咽异常的“功能”,然后您必须使用checkError()[实际执行此操作,以及谁只是假定写入成功...]进行显式检查。 This may or may not be desirable... 这可能是也可能不是......

Sure you can use a BufferedWriter . 当然你可以使用BufferedWriter PrintWriter is commonly used for conveniance as it offers a good range of functions without the extra exception handling (which often makes the examples easier). PrintWriter通常用于协调,因为它提供了一系列功能而无需额外的异常处理(这通常使示例更容易)。 The PrintWriter could also delegate its operations to a BufferedWriter if desired. 如果需要, PrintWriter还可以其操作委托BufferedWriter

Regarding performance see the javadocs for BufferedWriter 关于性能,请参阅BufferedWriter的javadoc

In general, a Writer sends its output immediately to the underlying character or byte stream. 通常,Writer会立即将其输出发送到基础字符或字节流。 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。

Whats wrong with PrintWriter? PrintWriter有什么问题? The match is made because of the convenient readLine / writeLine match. 匹配是因为方便的readLine / writeLine匹配。 You don't have that convenience in a BufferedWriter. 你在BufferedWriter中没有那么方便。 Also you get to specify autoflush with your PrintWriter. 您还可以使用PrintWriter指定autoflush。
If you need the buffer you can wrap the BufferedWriter in a PrintWriter 如果需要缓冲区,可以将BufferedWriter包装在PrintWriter中

PrintWriter pw = new PrintWriter( new BufferedWriter( ... ) );

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

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