How to connect a binary input stream to a file

A subset of the InputStream hierarchy

  • InputStream <<abstract>>
    • FileInputStream
    • FilterInputStream
      • BufferedInputStream
      • DataInputStream <implements DataInput interface>

Classes used to connect to a file with a buffer

  • DataInputStream – reads data from the stream
    • BufferedInputStream – creates a buffer for the stream
      • FileInputStream – connects the stream to the file

Constructors of these classes

Method Throws
DataInputStream(InutStream) None
BufferedInputStream(InputStream) None
FileInputStream(File) FileNotFoundException
FileInputStream(StringFileName) FileNotFoundException

Example 1: How to connect a binary input stream to a file

DataInputStream in = new DataInputStream(
new BufferedInputStream
new FileInputStream(productsFile));

Descriptions:

  • Although a buffer isn't required, it makes input operatios more efficient.

More info can be found here: http://www.j2ee.me/javase/6/docs/api/java/io/InputStream.html#InputStream()