Kim12
Kim12

Reputation: 23

What is the difference between DataInput/OutputStream and BufferedInput/ouputStream?

Let me preface this post with a single caution. I am a total beginner and i really want to learn and understand the best way to do these things. In addition to my top question, in what conditions can i use DataInput/OutputStream and BufferedInput/ouputStream? Thanks

Upvotes: 0

Views: 267

Answers (2)

Stelium
Stelium

Reputation: 1367

DataInput/OutputStream is used to read/write data like primitives (int, boolean etc) into files.

BufferedInput/ouputStream is a wrapper to increase performance.

In, fact you can use them both like this:

DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(file));

The whole design behind this is the Decorator Pattern. You can use multiple other decorators like ObjectOutputStream as wrappers.

Upvotes: 0

iceyb
iceyb

Reputation: 436

The DataInputStream works with the binary data, while the BufferedReader work with character data.

All primitive data types can be handled by using the corresponding methods in DataInputStream class, while only string data can be read from BufferedReader class and they need to be parsed into the respective primitives.

DataInputStream is a part of filtered streams, while BufferedReader is not.

DataInputStream consumes less amount of memory space being it is binary stream, where as BufferedReader consumes more memory space being it is character stream.

The data to be handled is limited in DataInputStream, where as the number of characters to be handled has wide scope in BufferedReader.

Upvotes: 1

Related Questions