In this quick article, you'll learn how to read a file using the FileInputStream class in Java. FileInputStream is a bytes stream class that can be used to read streams of raw bytes from a file.

Let us say we have the following input.txt file:

This
is
an
example
file.

Using FileInputStream Class

The following example demonstrates how you can use the FileInputStream class to read the above file, one byte at a time without buffering:

try {
    // create a reader
    FileInputStream fis = new FileInputStream(new File("input.txt"));

    // read one byte at a time
    int ch;
    while ((ch = fis.read()) != -1) {
        System.out.print((char) ch);
    }

    // close the reader
    fis.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

The above code will output the following:

This
is
an
example
file.

Since we are reading one byte at a time, the above program runs very slow for large-sized files. For better I/O performance, you can use the BufferedInputStream class to reduce the number of calls to the disk. BufferedInputStream reads a set of bytes at once into an internal byte array buffer of 8KB. Here is an example:

try {
    // create a reader
    FileInputStream fis = new FileInputStream(new File("input.txt"));
    BufferedInputStream reader = new BufferedInputStream(fis);

    // read one byte at a time
    int ch;
    while ((ch = reader.read()) != -1) {
        System.out.print((char) ch);
    }

    // close the reader
    reader.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

To specify a custom internal buffer size, do the following:

// custom buffer size
int BUFFER_SIZE = 32 * 1024; // 32KB

// create a reader
FileInputStream fis = new FileInputStream(new File("input.txt"));
BufferedInputStream reader = new BufferedInputStream(fis, BUFFER_SIZE);

For a file with different character encoding scheme, you can use the InputStreamReader class to wrap the FileInputStream object:

try {
    // create a reader
    FileInputStream fis = new FileInputStream(new File("input.txt"));
    
    // specify UTF_8 characer encoding
    InputStreamReader reader = new InputStreamReader(fis, StandardCharsets.UTF_8);

    // read one byte at a time
    int ch;
    while ((ch = reader.read()) != -1) {
        System.out.print((char) ch);
    }

    // close the reader
    reader.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

Using Files.newInputStream() Method

If you are using Java 8 or higher, you can the Files.newInputStream() static method to initialize a stream as shown below:

try {
    // create a reader
    InputStream is = Files.newInputStream(Paths.get("input.txt"));

    // read one byte at a time
    int ch;
    while ((ch = is.read()) != -1) {
        System.out.print((char) ch);
    }

    // close the reader
    is.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

Further Reading

You may be interested in other Java I/O articles:

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.