[英]Java socket output stream
Can you have an PrintWriter
and ObjectOutputStream
on the same sockets output stream? 你可以在同一个套接字输出流上有一个
PrintWriter
和ObjectOutputStream
吗?
out_stream = new PrintWriter(socket.getOutputStream(), true);
obj_stream = new ObjectOutputStream(socket.getOutputStream();
I would say yes but I don't think I would do it. 我会说是,但我不认为我会这样做。
What is it you want to do? 你想做什么?
You can but you have to take care of buffering. 你可以,但你必须照顾缓冲。 A
PrintWriter
or an ObjectOutputStream
accepts data, which it converts into bytes, to be sent on the underlying stream (here the socket) at some point . PrintWriter
或ObjectOutputStream
接受将其转换为字节的数据,以便在某个时刻在底层流(此处为套接字) 上发送。 Buffering is about waiting a bit before writing out such bytes, so that the bytes can be sent in "big chunks" rather than individually. 缓冲是在写出这样的字节之前稍等一下,这样字节就可以用“大块”而不是单独发送。
Read the Javadoc about buffering, and use flush()
on PrintWriter
and ObjectOutputStream
when you want to get sure that the bytes get written on the socket. 阅读关于缓冲的Javadoc,并在想要确保在套接字上写入字节时,在
PrintWriter
和ObjectOutputStream
上使用flush()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.