问题描述
我正在研究Android的聊天室应用程序。我读了不同的教程;其中一些使用PrintWriter发送数据,其中一些使用DataOutputStream。这两者有什么区别?哪一个更适合聊天应用?
I'm working on a chat room application for android. I read different tutorials; some of them use PrintWriter to send data and some of them use DataOutputStream. What is the difference between these two? Which one is better for a chat app?
推荐答案
来自java docs
From java docs
允许应用程序将原始Java数据类型写入以便携方式输出流。然后,应用程序可以使用数据输入流来重新读取数据。
A DataOutputStream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.
将对象的格式化表示形式打印到文本输出流。此类实现PrintStream中的所有打印方法。它不包含写入原始字节的方法,程序应该使用未编码的字节流。
PrintWriter Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
在一句话中,区别在于:
In one sentence the difference is:
OutputStreams用于二进制数据。作家(包括PrintWriter)用于文本数据
这篇关于PrintWriter和DataOutputStream有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!