Java 套接字:DataOutputStream 还是 OutputStream?

发布于 2024-11-28 05:50:39 字数 511 浏览 1 评论 0原文

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

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

socket.getOutputStream().write

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

  • 使用 DataOutputStream 或 OutputStream 什么被认为是“良好实践”? 我在互联网上找到的大多数 示例 使用 DataOutputStream (发送字符串) ,例如双向聊天)。
  • 与 OutputStream 相比,使用 DataOutputStream 有何优点或缺点?
  • 例如,在发送文件时,两者之间的性能是否存在明显差异?

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 DataOutputStream from the socket OutputStream and write to that.

  • What is considered "good practice", using a DataOutputStream or OutputStream?
    Most of the examples I 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦旅人picnic 2024-12-05 05:50:39

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

仅当传输原始二进制数据时才应使用 OutputStream

DataOutputStream makes 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.

You should use OutputStream only if you transfer raw binary data.

皓月长歌 2024-12-05 05:50:39

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

Use DataOutputStream if 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 BufferedOutputStream if you are doing small writes, and flush() when appropriate, i.e. before you read the socket for example.

謸气贵蔟 2024-12-05 05:50:39

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

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);

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