How to read a file using FileInputStream in Java

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.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.