I'm confused the above mentioned classes. When to use what? From my perspective every thing that comes in, is in the form of stream in java right? so which one is to use in what case to make the input more efficient? Also answer please can I use DataInputStream or BufferedInputStream in case of reading content from files?
-
Actually I have came accross that question but my query is different..as I have asked for which one is more suitable in which scenario and I have also queried for if could use DataInputStream in case reading file content.The tags are same not the question – Jul 26 '17 at 07:42
-
Streams use the decorator pattern, so most streams can be built on top of another, while others can be though as primitive stream which can be built on other objects (like FileInputStream, SocketInputStream....), refer to: https://stackoverflow.com/questions/6366385/decorator-pattern-for-io – minus Jul 26 '17 at 07:43
-
1Yeah, but if you read the answer, it should answer your question. `DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream("filename.dat")));` They are all quite different. – matt Jul 26 '17 at 07:45
1 Answers
FileInputStream
Is used for reading from files.
See the JavaDoc:
A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. [...]
DataInputStream
Is used for reading in primitive Java types (that you might have written using a DataOutputStream
) and provides convenience methods for that purpose, e.g. writeInt()
.
See the JavaDoc:
A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. [...]
BufferedInputStream
Is used to do buffered block reads from an InputStream
(instead of single bytes) and increases performance if reading small chunks of data. Most of the time you want to use it for text processing.
See the JavaDoc:
A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input[...].
Of course you can combine those as they are following the Decorator Pattern.
Example of writing primitive Java types to a file:
FileOutputStream write = new FileOutputStream
DataOutputStream out = new DataOutputStream(write);
out.writeInt(10);
write.close();

- 4,247
- 5
- 31
- 74
-
`BufferedInputStream` is not a replacement for `DataInputStream`. As I mentioned you can combine those if you want, but their purpose is completely different. The one is for conveniently writing primitive data, the other one might be used for performance improvements. – JDC Jul 26 '17 at 07:50
-
-
-
Correct. See here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html – JDC Jul 26 '17 at 07:59
-
Please don't get annoyed...as you mentioned FileInputStream is for getting for data from files then when DataInputStream is recquired? Yes I can combine them but when I can only use DataInputStream ? – Jul 26 '17 at 08:12
-
1You won't be able to use it standalone, as you have to provide another inputstream to the constructor. The `DataInputStream` is just a helpful class to handle some other `InputStream` and read data from it in a convenient way. But you have to pass some source stream to the `DataInputStream`. – JDC Jul 26 '17 at 08:17