ZetCode

Java FileInputStream

last modified January 10, 2023

Java FileInputStream tutorial shows how to use FileInputStream class to read files in Java.

Java FileInputStream

FileInputStream reads input bytes from a file in a file system.

Java FileInputStream constructors

These are FileInputStream constructors:

Java FileInputStream close

The FileInputStream's close method closes the file input stream and releases any system resources associated with this stream. In our examples we use try-with-resources statement, which ensures that each resource is closed at the end of the statement.

Java FileInputStream read

FileInputStream reads bytes with the following read methods :

Java FileInputStream reading characters

The following example uses FileInputStream to read three characters from a file.

FileInputStreamEx.java
package com.zetcode;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStreamEx {

    public static void main(String[] args) throws FileNotFoundException, IOException {

        String fileName = "/home/janbodnar/tmp/smallfile.txt";

        try (FileInputStream fis = new FileInputStream(fileName)) {

            char c1 = (char) fis.read();
            char c2 = (char) fis.read();
            char c3 = (char) fis.read();

            System.out.println(c1);
            System.out.println(c2);
            System.out.println(c3);
        }
    }
}

The code example reads three characters with read.

char c1 = (char) fis.read();

We read a character with read and cast the value into a char.

System.out.println(c1);

The character is printed to the console.

Java FileInputStream reading file by characters

The read method returns -1 if the end of the file is reached. With a while loop we can read a whole file character by character. Note that this way is not very efficient.

FileInputStreamEx2.java
package com.zetcode;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStreamEx2 {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        
        String fileName = "/home/janbodnar/tmp/smallfile.txt";

        try (FileInputStream fis = new FileInputStream(fileName)) {

            int i; 
            
            while ((i = fis.read()) != -1) {
                System.out.print((char) i);
            }
        }        
        
        System.out.println();
    }
}

The example reads the contents of a file and writes it to the terminal.

while ((i = fis.read()) != -1) {
    System.out.print((char) i);
}

In a while loop we read a character from a FileInputStream until the read method returns -1.

Java FileInputStream reading file by text chunks

It is more efficient to read a file by data chunks; for instance 1024 bytes in each method call.

FileInputStreamEx3.java
package com.zetcode;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class FileInputStreamEx3 {

    public static void main(String[] args) throws FileNotFoundException, IOException {

        String fileName = "/home/janbodnar/tmp/bigfile.txt";

        try (FileInputStream fis = new FileInputStream(fileName)) {

            int i = 0;

            do {

                byte[] buf = new byte[1024];
                i = fis.read(buf);
                
                String value = new String(buf, StandardCharsets.UTF_8);
                System.out.print(value);

            } while (i != -1);
        }
    }
}

In this example we read a file by data chunks

byte[] buf = new byte[1024];

We read data from a file into this array of bytes.

i = fis.read(buf);

The read method reads up to b.length bytes of data from this the stream into the provided array of bytes.

String value = new String(buf, StandardCharsets.UTF_8);

From the array of bytes, we create a String.

Java FileInputStream with BufferedReader

Reading is more efficient with BufferedReader. BufferedReader reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters.

FileInputStreamEx4.java
package com.zetcode;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class FileInputStreamEx4 {

    public static void main(String[] args) throws FileNotFoundException, IOException {

        String fileName = "/home/janbodnar/tmp/bigfile.txt";

        try (BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(fileName), StandardCharsets.UTF_8));) {

            String line;
            
            while ((line = br.readLine()) != null) {
                
                System.out.println(line);
            }
        }

        System.out.println();
    }
}

The example reads a big file using buffering technique for greater efficiency.

while ((line = br.readLine()) != null) {

The readLine method reads a line of text from a buffer.

In this article, we have presented the Java FileInputStream class.

List all Java tutorials.