Difference Between DataInputStream/DataOutputStream Class & InputStream/OutputStream Class

本小妞迷上赌 提交于 2019-12-04 13:47:43

问题


Whenever I use HttpConnection Class in Java ME, Android or in BlackBerry, I uses DataInputStream/DataOutputStream class for reading & writing datas over remote server. However there are other class like InputStream/OutputStream which can be use for same purpose. I saw Question regarding InputStream/OutputStream class with HttpConnection. So I would like to know from experts that what are the differences between these two ?


回答1:


DataInputStream/DataOutputStream isa InputStream/Outputstream. InputStream and OutputStream are the most generic IO streams you can use and they are the base class of all streams in Java. You can read and write raw bytes only with them. DataInputStream writes formatted binary data. Instead of just simple unformatted bytes you can read Bytes, Integer, Double, Float, Short, UTF-8 strings, and any mixture of that data. And same can be said for DataOutputStream except that it writes these higher level data types. A DataInputStream/DataOutputStream has a reference to an InputStream/OutputStream which it reads the raw bytes and interprets those bytes as those previously mentioned data types.

Although reading strings from the DataInputStream isn't a good idea because it makes unchangeable assumptions about the character encoding of the underlying InputStream. Instead it's better to use a Reader which will properly apply character encodings to the underlying byte stream to read data. That's why DataInputStream/DataOutputStream is of limited use. Typically it's better to trade textual data between processes because it's easiest to make a server and client agree on how to parse the data. Trading binary has lots of bit twiddling that has to occur to make sure each process is talking the same language. Its easy if you have two Java processes using DataInputStream/DataOutputStream, but if you ever want to add a new client that isn't Java you'll have a harder time reusing it. Not impossible, but just harder.




回答2:


DataOutputStream makes sure the data is formatted in a platform independent way

OutputStream only if you transfer raw binary data.

DataOutputStream-This is the big benefit.

There is no significant performance difference between both.




回答3:


DataOutputStream can only handle basic types.

It can only read/write primtive types and Strings.DataInput/OutputStream performs generally better because its much simpler.

ObjectInput/OutputStream can read/write any object type was well as primitives. It is less efficient but much easier to use if you want to send complex data.

With the ObjectOutputStream class, instances of a class that implements Serializable can be written to the output stream, and can be read back with ObjectInputStream.

I would assume that the Object*Stream is the best choice until you know that its performance is an issue.



来源:https://stackoverflow.com/questions/8921225/difference-between-datainputstream-dataoutputstream-class-inputstream-outputst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!