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




Java IO: FileInputStream

Jakob Jenkov
Last update: 2014-11-15

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

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).

close()

Just like any other InputStream a FileInputStream needs to be closed properly after use. You do that by calling the FileInputStream's close() method. You can see that in the FileInputStream example further up this text, and remember to check the Java IO exception handling tutorial for more information about proper exception handling (link below the example further up the page).

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC