Tech and Media Labs
This site uses cookies to improve the user experience.


Java IO: FileInputStream

Jakob Jenkov
Last update: 2018-09-19

The Java FileInputStream class makes it possible to read the contents of a file as a stream of bytes. The Java FileInputStream class is a subclass of Java InputStream. This means that you use the Java FileInputStream as an InputStream (FileInputStream behaves like an InputStream).

Java FileInputStream Example

Here is a simple FileInputStream example:

InputStream input = new FileInputStream("c:\\data\\input-text.txt");

int data = input.read();
while(data != -1) {
  //do something with data...
  doSomethingWithData(data);

  data = input.read();
}
input.close();

Note: The proper exception handling has been skipped here for the sake of clarity. To learn more about correct exception handling, go to Java IO Exception Handling.

FileInputStream Constructors

The FileInputStream class has a three different constructors you can use to create a FileInputStream instance. I will cover the first two here.

The first constructor takes a String as parameter. This String should contain the path in the file system to where the file to read is located. Here is a code example:

String path = "C:\\user\\data\\thefile.txt";

FileInputStream fileInputStream = new FileInputStream(path);

Notice the path String. It needs double backslashes (\\) to create a single backslash in the String, because backslash is an escape character in Java Strings. To get a single backslash you need to use the escape sequence \\.

On unix the file path could have looked like this:

String path = "/home/jakobjenkov/data/thefile.txt";

Notice the use of the for-slash (the normal slash character) as directory separator. That is how you write file paths on unix. Actually, in my experience Java will also understand if you use a / as directory separator on Windows (e.g. c:/user/data/thefile.txt), but don't take my word for it. Test it on your own system!

The second FileInputStream constructor takes a File object as parameter. The File object has to point to the file you want to read. Here is an example:

String path = "C:\\user\\data\\thefile.txt";
File   file = new File(path);

FileInputStream fileInputStream = new FileInputStream(file);

Which of the constructors you should use depends on what form you have the path in before opening the FileInputStream. If you already have a String or File, just use that as it is. There is no particular gain in converting a String to a File, or a File to a String first.

read()

The read() method of a FileInputStream returns an int which contains the byte value of the byte read. If the read() method returns -1, there is no more data to read in the FileInputStream, and it can be closed. That is, -1 as int value, not -1 as byte value. There is a difference here!

You use the read() method just like the read() method of an InputStream.

read(byte[])

Being an InputStream the FileInputStream also has two read() methods which can read data into a byte array. You can read more about them in my tutorial about the InputStream (see link elsewhere in this text).

Closing a FileInputStream

When you are finished reading data from a Java FileInputStream you must close it. You close a FileInputStream by calling the close() method inherited from InputStream. Here is an example of opening a FileInputStream, reading all data from it, and then closing it:

FileInputStream fileInputStream = new FileInputStream("c:\\data\\input-text.txt");

int data = fileInputStream.read();
while(data != -1) {
  data = fileInputStream.read();
}
fileInputStream.close();

Notice how the while loop continues until a -1 value is read from the FilenputStream read() method. After that, the while loop exits, and the FileInputStream close() method is called.

The above code is not 100% robust. If an exception is thrown while reading data from the FileInputStream, the close() method is never called. To make the code more robust, you will have to use the Java Java try with resources construct. Proper exception handling for use of Java IO classes is also explained in my tutorial on Java IO Exception Handling.

Here is an example of closing a Java FileInputStream using the try-with-resources construct:

try( FileInputStream fileInputStream = new FileInputStream("file.txt") ) {

    int data = fileInputStream.read();
    while(data != -1){
        data = fileInputStream.read();
    }
}

Notice that the FileInputStream is declared inside the parantheses after the try keyword. This signals to Java that this FileInputStream is to be managed by the try-with-resources construct.

Once the executing thread exits the try block, the FileInputStream variable is closed. If an exception is thrown from inside the try block, the exception is caught, the FileInputStream is closed, and then the exception is rethrown. You are thus guaranteed that the FileInputStream is closed, when used inside a try-with-resources block.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC