新 PrintWriter(新 BufferedWriter(新 PrintWriter(s.getOutputStream, true)))

发布于 2025-01-07 02:39:36 字数 299 浏览 0 评论 0原文

我想知道在 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 技术交流群。

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

发布评论

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

评论(2

送君千里 2025-01-14 02:39:36

这是合法的,但很笨拙。您可以改为缓冲 OutputStream

new PrintWriter(new BufferedOutputStream(s.getOutputStream), true)

另外看看 new PrintWriter(OutputStream, boolean) 的实现:

public PrintWriter(OutputStream out, boolean autoFlush) {
  this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
}

缓冲已经存在了!

It is legal but clumsy. You can buffer OutputStream instead:

new PrintWriter(new BufferedOutputStream(s.getOutputStream), true)

Also have a look at the implementation of new PrintWriter(OutputStream, boolean):

public PrintWriter(OutputStream out, boolean autoFlush) {
  this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
}

buffering is already there!

谈下烟灰 2025-01-14 02:39:36

OutputStreamWriter 是您正在寻找的类。只需向其传递一个流和一个编码,例如 "UTF-8"

new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), encoding)), true) 

OutputStreamWriter is the class you're looking for. Just pass it a stream and an encoding, for example "UTF-8".

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