Java 套接字:DataOutputStream 还是 OutputStream?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6984130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:09:09  来源:igfitidea点击:

Java sockets: DataOutputStream or OutputStream?

javasocketsnetworkingdataoutputstream

提问by David

I'm still relatively new to sockets, and I haven't seen any information regarding this subject.

我对套接字还是比较陌生,我还没有看到任何关于这个主题的信息。

To write to a connected socket, you can either use

要写入连接的套接字,您可以使用

socket.getOutputStream().write

Or create a new DataOutputStreamfrom the socket OutputStreamand write to that.

或者DataOutputStream从套接字创建一个新的OutputStream并写入。

  • What is considered "good practice", using a DataOutputStream or OutputStream? Most of the examplesI find on the internet use DataOutputStream (to send Strings, such as in a two way chat).
  • Are there any advantages or disadvantages from using DataOutputStream over OutputStream?
  • Is there any difference in performance that is noticeable between these two when, for example, sending files?
  • 使用 DataOutputStream 或 OutputStream 时,什么被认为是“良好实践”?我在互联网上找到的大多数示例都使用 DataOutputStream(发送字符串,例如双向聊天)。
  • 使用 DataOutputStream 而不是 OutputStream 有什么优点或缺点吗?
  • 例如,在发送文件时,这两者之间是否存在明显的性能差异?

采纳答案by Jér?me Verstrynge

DataOutputStreammakes sure the data is formatted in a platform independent way. This is the big benefit. It makes sure the party on the other side will be able to read it. There is no significant performance difference between both.

DataOutputStream确保数据以独立于平台的方式格式化。这是最大的好处。它确保另一方的一方能够阅读它。两者之间没有显着的性能差异。

You should use OutputStreamonly if you transfer raw binary data.

OutputStream只有在传输原始二进制数据时才应该使用。

回答by user207421

Use DataOutputStreamif you need the extra APIs. If you don't, there is no point. But you should always wrap the socket's output stream in a BufferedOutputStreamif you are doing small writes, and flush()when appropriate, i.e. before you read the socket for example.

DataOutputStream如果您需要额外的 API,请使用。如果你不这样做,那就没有意义了。但是,BufferedOutputStream如果您正在执行小写操作,并且flush()在适当的时候,例如在读取套接字之前,您应该始终将套接字的输出流包装在 a 中。

回答by FaisalC

Just now I came to know a difference between dataoutputstream and outputstreamwriter while working with a SOAP services... I tried to pass arabic data through request XML but in the response XML I'm getting some junk characters in place of arabic data then I tried to encode (UTF-8) the request but there is no such method to encode in DataOutputStream where as you can encode the request in OutputStreamWriter before sending the request. OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(), "UTF-8"); out.write(inputXML);

刚才我在使用 SOAP 服务时开始知道 dataoutputstream 和 outputstreamwriter 之间的区别......我试图通过请求 XML 传递阿拉伯数据,但在响应 XML 中我得到一些垃圾字符代替阿拉伯数据然后我尝试对请求进行编码(UTF-8),但没有这样的方法可以在 DataOutputStream 中进行编码,因为您可以在发送请求之前在 OutputStreamWriter 中对请求进行编码。OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(), "UTF-8"); out.write(inputXML);