Java 客户端/服务器 - 使用 BufferedWriter 代替 PrintWriter
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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...
PrintWriter 有什么问题吗?进行匹配是因为方便的 readLine / writeLine 匹配。在 BufferedWriter 中你没有这样的便利。您还可以使用 PrintWriter 指定自动刷新。
如果您需要缓冲区,可以将 BufferedWriter 包装在 PrintWriter 中
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
当然,您可以使用 BufferedWriter。
PrintWriter
通常用于方便,因为它提供了一系列良好的功能,而无需额外的异常处理(这通常使示例更容易)。如果需要,PrintWriter
还可以将其操作委托给BufferedWriter
。关于性能,请参阅
BufferedWriter
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). ThePrintWriter
could also delegate its operations to aBufferedWriter
if desired.Regarding performance see the javadocs for
BufferedWriter