问题描述
我对套接字仍然比较新,我还没有看到有关此主题的任何信息。
要写入已连接的套接字,您可以使用
socket.getOutputStream()。write
或者从套接字 OutputStream
创建一个新的 DataOutputStream
并写入。
- 什么被认为是良好做法,使用DataOutputStream或OutputStream?
我在互联网上找到的大部分都使用DataOutputStream (发送字符串,如双向聊天)。 - 使用DataOutputStream而不是OutputStream有什么优点或缺点吗?
- 这两者之间的性能是否存在差异,例如,发送文件?
DataOutputStream
确保以独立于平台的方式格式化数据。这是一个很大的好处。它确保对方的一方能够阅读它。两者之间没有显着的性能差异。
只有在传输原始二进制数据时才应使用 OutputStream
。 / p>
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?
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.
这篇关于Java套接字:DataOutputStream还是OutputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!