# How to read a file using FileInputStream in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 6, 2019. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-read-file-using-fileinputstream

> A short tutorial to learn how to read and write 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:

```plaintext
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:

```java
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:

```bash
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:

```java
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:

```java
// 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:

```java
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:

```java
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:

* [Reading and Writing Files in Java](https://attacomsian.com/blog/java-read-write-files)
* [How to Read and Write Text Files in Java](https://attacomsian.com/blog/java-read-write-text-files)
* [How to Read and Write Binary Files in Java](https://attacomsian.com/blog/java-read-write-binary-files)
* [Reading and Writing Files using Java NIO API](https://attacomsian.com/blog/java-read-write-files-nio-api)
* [How to read a file line by line in Java](https://attacomsian.com/blog/how-to-read-a-file-line-by-line-in-java)
