Java 客户端/服务器 - 使用 BufferedWriter 代替 PrintWriter

发布于 2024-10-20 13:28:16 字数 463 浏览 2 评论 0原文

在 Java 客户端/服务器的所有示例中,我都看到 BufferedReader 用于接收数据,如 in

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

PrintWriter 用于发送数据,如 in

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

但我不能使用 BufferedWriter 而不是 PrintWriter?我只需要在客户端和服务器之间发送未格式化的字符串,因此 BufferedWriter 应该提供更好的性能(这并不是一个问题)。

In all examples of a Java client/server, I've seen BufferedReader used for receiving data, like in

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

and PrintWriter for sending data, like in

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

But can't I just use a BufferedWriter instead of a 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧情勿念 2024-10-27 13:28:16

PrintWriter 本质上提供了围绕 Writer 的便捷方法。如果您不需要这些便捷方法,而只需要编写字符,那么从功能上讲,您可以使用您选择的任何风格的 Writer,包括“原始”OutputStreamWriter。

如果您一次写入一个字符,并且您的套接字流没有缓冲,那么建议在某处放置一些缓冲,可以使用 BufferedWriter 或将 BufferedOuputStream 包装在原始输出流周围。您通常不需要这样做的一个示例是在 servlet 中,其中传递到 servlet 的流通常已经被缓冲。

PrintWriter 还具有吞咽写入方法上的异常的“功能”,然后您必须使用 checkError() 显式检查该异常[举起谁实际执行此操作,以及谁只是假设写入成功...]。这可能是理想的,也可能不是理想的......

PrintWriter essentially provides convenience methods around a 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.

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. 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.

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...]. This may or may not be desirable...

挽你眉间 2024-10-27 13:28:16

PrintWriter 有什么问题吗?进行匹配是因为方便的 readLine / writeLine 匹配。在 BufferedWriter 中你没有这样的便利。您还可以使用 PrintWriter 指定自动刷新。
如果您需要缓冲区,可以将 BufferedWriter 包装在 PrintWriter 中

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

Whats wrong with PrintWriter? The match is made because of the convenient readLine / writeLine match. You don't have that convenience in a BufferedWriter. Also you get to specify autoflush with your PrintWriter.
If you need the buffer you can wrap the BufferedWriter in a PrintWriter

PrintWriter pw = new PrintWriter( new BufferedWriter( ... ) );
榆西 2024-10-27 13:28:16

当然,您可以使用 BufferedWriter。 PrintWriter 通常用于方便,因为它提供了一系列良好的功能,而无需额外的异常处理(这通常使示例更容易)。如果需要,PrintWriter 还可以将其操作委托BufferedWriter

关于性能,请参阅 BufferedWriter

一般来说,Writer 会立即将其输出发送到底层字符或字节流。除非需要提示输出,否则建议将 BufferedWriter 包装在任何 write() 操作可能成本较高的 Writer 周围,例如 FileWriters 和 OutputStreamWriters。

Sure you can use a 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). The PrintWriter could also delegate its operations to a BufferedWriter if desired.

Regarding performance see the javadocs for BufferedWriter

In general, a Writer sends its output immediately to the underlying character or byte stream. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文