Need for data input stream

What's 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?

+6
source share
5 answers

The significant thing about the object passed to the InputStreamReader() constructor is that it will be an object that will carry the weight of any synchronization. If you do not want your FileInputStream potentially delayed by many calls to it, then the second option is the way to go. See source Reader .

+2
source

Using a DataInputStream is a common mistake, which I believe is related to copying and pasting from different parts of the code. You want to read files as text, for example. BufferedReader OR binary e.g. DataInputStream. Its very unlikely that you want to use both, and this will probably lead to confusion.

For text buffered

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

For binary code buffered

 DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file))); 
+4
source

FileInputStream is designed to read streams of raw bytes, such as image data. To read character streams, consider using FileReader.

The input data stream allows the application to read Java primitive data types from the underlying input stream in a machine-independent manner. the application uses the output data stream to write data that can subsequently be read by the data input stream.

DataInputStream is not necessarily secure for multi-threaded access.

FileInputStream gives you a very simple interface. When you want to read numbers, strings (or even complex objects), and not just bytes, it's a pain. Thus, you use the second input stream, "wrapping" the first, which gives you a more convenient interface. DataInputStream is one of them.

+2
source

It depends, the data input stream allows the application to read Java primitive data types from the underlying input stream in a machine-independent manner. The application uses the output data stream to write data that can subsequently be read by the data input stream. Check JavaDoc

BufferedReader : reads text from a character input stream, buffers characters to provide efficient reading of characters, arrays, and strings.

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

+1
source

No, in your example there is no need for a DataInputStream, because you finally get a BufferedReader for reading data.

What's the point:

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

This will usually go hand in hand if you created file1.txt using:

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

Edit:

Why is it allowed by Java, if that doesn't make sense? Because it is a Decorator , and this is one of the drawbacks of the Decorator template.

+1
source

Source: https://habr.com/ru/post/920657/


All Articles