问题描述
我可以在同一输出流上同时使用PrintWriter和BufferedOutputStream吗?
Can I use both PrintWriter and BufferedOutputStream on the same outputstream?
情况如下:
我有一个SSLSocket程序,我希望能够在不同的时间点轻松地在套接字上传输字符串和byte [],而不必将byte []转换为字符串等,但是,PrintWriter只允许我传输字符串和BufferedOutputStream只允许我传输字节。
I'm having a SSLSocket program and I want to be able to transfer both Strings and byte [] over the socket at different points in time easily without having to cast byte [] to string etc, however, PrintWriter only allows me to transfer Strings and BufferedOutputStream only allows me to transfer bytes.
推荐答案
PrintWriter只是原始OutputStream的包装器。
BufferedOutputStream也是OutputStream的包装器。
所以你可以使用两者。为什么不呢?
PrintWriter is just a wrapper around original OutputStream.BufferedOutputStream is a wrapper around OutputStream too.So you can use both. Why not?
您应该考虑的一件事 - 您应该小心在多线程环境中使用流和编写器,因为您可能会得到不可预测的结果。
Single thing you should to consider - you should be carefull working with both stream and writer in multithread environment because you can get unpredictable result.
java.io.Writer类(PrintWriter的超类)将自身用作锁对象。所以你也可以用它来同步多个线程。
The java.io.Writer class (super class for PrintWriter) use itself as a lock object. So you can also probably use it to synchronize multiple threads.
这篇关于我可以在同一输出流上同时使用PrintWriter和BufferedOutputStream吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!