In Java, we use FileInputStream to read bytes from a file, such as an image file or binary file.

Note

However, all the below examples use FileInputStream to read bytes from a text file and print them out. The text file allows readers to “see” the output correctly. Generally, we use Reader to read characters from a text file.

1. FileInputStream – Read a file

This example uses FileInputStream to read bytes from a file and print out the content. The fis.read() reads a byte at a time, and it will return a -1 if it reached the end of the file.

FileInputStreamExample1.java

package com.favtuts.io.api.inputstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamExample1 {

    public static void main(String[] args) {
        String fileName = "/home/tvt/workspace/favtuts/writefile.txt";

        readFile(fileName);
    }
    
    private static void readFile(String fileName) {

        try (FileInputStream fis = new FileInputStream(new File(fileName))) {
            int content;
            // reads a byte at a time, if it reached end of the file, returns -1
            while((content = fis.read()) != -1) {
                System.out.println((char)content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2. FileInputStream – Remaining bytes

We can use fis.available() to check the remaining bytes that can be read. For example:

Below is a text file containing 10 bytes.

c:\\test\\file.txt

helloworld

FileInputStreamExample2.java

package com.favtuts.io.api.inputstream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamExample2 {

    public static void main(String[] args) {
        String fileName = "/home/tvt/workspace/favtuts/test.txt";

        readFile(fileName);
    }
    
    private static void readFile(String fileName) {

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

            // remain bytes that can be read
            System.out.println("Remaining bytes that can be read : " + fis.available());

            int content;
            // reads a byte at a time, if it reached end of the file, returns -1
            while((content = fis.read()) != -1) {
                System.out.println((char)content);

                System.out.println("Remaining bytes that can be read : " + fis.available());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Output

Remaining bytes that can be read : 10
h
Remaining bytes that can be read : 9
e
Remaining bytes that can be read : 8
l
Remaining bytes that can be read : 7
l
Remaining bytes that can be read : 6
o
Remaining bytes that can be read : 5
w
Remaining bytes that can be read : 4
o
Remaining bytes that can be read : 3
r
Remaining bytes that can be read : 2
l
Remaining bytes that can be read : 1
d
Remaining bytes that can be read : 0

For a file containing 10 bytes file, the fis.read() will run ten times and read a byte for each time. (See the problem here?)

3. FileInputStream – Better performance

3.1 Review the FileInputStream#read() source code, each read() will call the native method to read a byte from the disk.

package java.io;

public class FileInputStream extends InputStream {

    /**
     * Reads a byte of data from this input stream. This method blocks
     * if no input is yet available.
     *
     * @return     the next byte of data, or <code>-1</code> if the end of the
     *             file is reached.
     * @exception  IOException  if an I/O error occurs.
     */
    public int read() throws IOException {
        return read0();
    }

    private native int read0() throws IOException;

    //...
}

3.2 We can use the read(byte b[]) to read predefined bytes into a byte array; it will significantly increase the read performance.

FileInputStreamExample3.java

package com.favtuts.io.api.inputstream;

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

public class FileInputStreamExample3 {

    public static void main(String[] args) {
        String fileName = "/home/tvt/workspace/favtuts/test.txt";

        readFileBetterPerformance(fileName);
    }

    private static void readFileBetterPerformance(String fileName) {
        try (FileInputStream fis = new FileInputStream(new File(fileName))) {

            // remaining bytes that can be read
            System.out.println("Remaining bytes that can be read : " + fis.available());

            // 8k a time
            byte[] bytes = new byte[8192];

            // reads 8192 bytes at a time, if end of the file, returns -1
            while (fis.read(bytes) != -1) {

                // convert bytes to string for demo
                System.out.println(new String(bytes, StandardCharsets.UTF_8));

                System.out.println("Remaining bytes that can be read : " + fis.available());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

The above example will read 8192 bytes at a time, and for a file containing 10 bytes, it reads only one time.

Remaining bytes that can be read : 10
helloworld
Remaining bytes that can be read : 0

Note

For example, if a file containing 81920 bytes (80 kb), the default fis.read will require an 81920 native calls to read all bytes from the file; While the fis.read(bytes) (for a size of 8192), we only need 10 native calls. The difference is enormous.

4. FileInputStream vs BufferedInputStream

The FileInputStream reads a byte at a time, and each read() will be a native read from the disk. For reading a large file, it will slow.

The BufferedInputStream reads 8192 bytes (default) at a time and buffers them until they are needed; The BufferedInputStream#read() still returns a single byte at a time, but other remaining bytes are in the buffer and reserved for the next read. The concept is similar to the above FileInputStreamExample3.java

The common practice uses BufferedInputStream to wrap the FileInputStream to provide a buffer cache to increase the read performance.

FileInputStreamExample4.java

package com.favtuts.io.api.inputstream;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamExample4 {

    public static void main(String[] args) {
        String fileName = "/home/tvt/workspace/favtuts/test.txt";

        readFileBetterPerformance2(fileName);
    }

    private static void readFileBetterPerformance2(String fileName) {
        try (BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream(new File(fileName)))) {

            // remaining bytes that can be read
            System.out.println("Remaining bytes that can be read : " + bis.available());

            int content;
            // reads 8192 bytes at a time and buffers them until they are needed,
            // if end of the file, returns -1
            while ((content = bis.read()) != -1) {

                // convert bytes to string for demo
                System.out.println((char) content);

                System.out.println("Remaining bytes that can be read : " + bis.available());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output

Remaining bytes that can be read : 10
h
Remaining bytes that can be read : 9
e
Remaining bytes that can be read : 8
l
Remaining bytes that can be read : 7
l
Remaining bytes that can be read : 6
o
Remaining bytes that can be read : 5
w
Remaining bytes that can be read : 4
o
Remaining bytes that can be read : 3
r
Remaining bytes that can be read : 2
l
Remaining bytes that can be read : 1
d
Remaining bytes that can be read : 0

5. Convert FileInputStream to Reader

It’s also common to use InputStreamReader to convert InputStream to a Reader.

This example shows how to convert a FileInputStream to BufferedReader, and read it line by line.

private static void readFileBetterInputStreamReader(String fileName) {

    try (BufferedReader br =
                 new BufferedReader(
                         new InputStreamReader(
                                 new FileInputStream(new File(fileName))))) {

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

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

}

6. FileInputStream – Read a Unicode file

This example uses FileInputStream to read a Unicode file. For example:

A Unicode file containing a few Chinese characters, and each Unicode code character contains two or more bytes.

c:\\test\\file-unicode.txt

你好
我好
大家好

We can use the above example 3 to read the Unicode file and print them correctly.

FileInputStreamExample6.java

package com.favtuts.io.api.inputstream;

import java.io.*;
import java.nio.*;
import java.util.*;

public class FileInputStreamExample {

    public static void main(String[] args) {

        String fileUnicode = "/home/tvt/workspace/favtuts/file-unicode.txt";
        List<String> lines = Arrays.asList("你好", "我好", "大家好");
        writeUnicodeClassic(fileUnicode, lines);
        readFileBetterPerformance(fileUnicode);
    }

    private static void readFileBetterPerformance(String fileName) {
        try (FileInputStream fis = new FileInputStream(new File(fileName))) {

            // remaining bytes that can be read
            System.out.println("Remaining bytes that can be read : " + fis.available());

            // 8k a time
            byte[] bytes = new byte[8192];

            // reads 8192 bytes at a time, if end of the file, returns -1
            while (fis.read(bytes) != -1) {

                // convert bytes to string for demo
                System.out.println(new String(bytes, StandardCharsets.UTF_8));

                System.out.println("Remaining bytes that can be read : " + fis.available());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // in the old days
    public static void writeUnicodeClassic(String fileName, List<String> lines) {

        File file = new File(fileName);

        try (FileOutputStream fos = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
            BufferedWriter writer = new BufferedWriter(osw)
        ) {
            Integer idx = 0;
            for (String line : lines) {
                idx += 1;
                writer.append(line);
                                
                //No append new Line with the last item
                if (idx < lines.size())
                    writer.newLine();
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output

Remaining bytes that can be read : 23
你好
我好
大家好
Remaining bytes that can be read : 0

Furthermore, we also can use the example 5 InputStreamReader to read and print the Unicode file, by default the InputStreamReader has a default charset of UTF-8.

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-io/api

References

Leave a Reply

Your email address will not be published. Required fields are marked *