新 PrintWriter(新 BufferedWriter(新 PrintWriter(s.getOutputStream, true)))
我想知道在 Java 中是否可以做到
new PrintWriter(new BufferedWriter(new PrintWriter(s.getOutputStream, true)))
s 是 Socket?因为不可能从输出流创建 BufferedWriter,所以我用 PrintWriter 包装了输出流。但我想缓冲打印输出,所以我用 BufferedWriter 包装它。但最终我想使用 printWriter 进行打印,所以我用 PrintWriter 再次包装它。这在 Java 中合法吗?谢谢!
I am wondering is it possible to do
new PrintWriter(new BufferedWriter(new PrintWriter(s.getOutputStream, true)))
in Java where s is a Socket? Because it's impossible to create a BufferedWriter from an outputstream, I have wrapped the outputstream with a PrintWriter. But I want to buffer my print outs, so I wrap it with a BufferedWriter. But eventually I want to print using printWriter so I wrap it again with a PrintWriter. Is this legal in Java? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是合法的,但很笨拙。您可以改为缓冲
OutputStream
:另外看看
new PrintWriter(OutputStream, boolean)
的实现:缓冲已经存在了!
It is legal but clumsy. You can buffer
OutputStream
instead:Also have a look at the implementation of
new PrintWriter(OutputStream, boolean)
:buffering is already there!
OutputStreamWriter
是您正在寻找的类。只需向其传递一个流和一个编码,例如"UTF-8"
。OutputStreamWriter
is the class you're looking for. Just pass it a stream and an encoding, for example"UTF-8"
.