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?
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)));
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With