Question

What is the difference between

FileInputStream fstream = new FileInputStream ("file1.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

and

FileInputStream fstream = new FileInputStream ("file1.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

Do we really need a DataInputStream here?

Was it helpful?

Solution

The significant thing about the object passed to the InputStreamReader() constructor is that it will be the object that will bear the weight of any synchronization holds. If you don't want your FileInputStream to potentially be held up by many calls to it, then the second option is the way to go. See the source of Reader.

OTHER TIPS

The use of DataInputStream is a common mistake which I believe comes from copy-and-paste from different pieces of code. You want to read the files as either text e.g. BufferedReader OR binary e.g. DataInputStream. Its highly unlikely you want to use both and trying to is likely to lead to confusion.

For Text which is buffered

BufferedReader br = new BufferedReader(new FileReader(file));

For binary which is buffered

DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));

FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.

DataInputStream is not necessarily safe for multithreaded access.

FileInputStream only gives you a very basic interface. When you're wanting to read numbers, Strings (or even complex Objects) rather than just bytes, that's a pain. So you use a second input stream "wrapping" the first, which gives you a more useful interface. DataInputStream is one of these.

It depends, A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream. Check JavaDoc

BufferedReader : Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

FileInputStream : Using FileInputStream, you will read file data in bytes.

No, there is no need for DataInputStream in your example because you are finally getting a BufferedReader to read data.

What would instead make sense is:

FileInputStream fstream = new FileInputStream ("file1.txt");
BufferedInputStream br = new BufferedInputStream(fstream);
DataInputStream dis = new DataInputStream(br);

Usually this would go hand in hand when you have created "file1.txt" using:

DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("file1.txt")));

Edit:

Why is it allowed by Java if it doesn't really make sense? Because this is Decorator pattern and this is one of the disadvantages of Decorator pattern.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top