Write a program to read contents of a file using FileInputStream :

‘FileInputStream’ is used to read contents of file as raw bytes. Actually this is not the best way to read characters of a file. Mainly for reading other files like images, songs etc. ‘FileInputStream’ is used. For reading characters of a file, you can use ‘FileReader’ . Anyway, I will show you how to read characters of a file using this class . This will give you a basic overview of ‘FileInputStream’.

Creating a object of FileInputStream :

To create a object of ‘FileInputStream’, we have three different ways. (i.e. three different constructors available). We can create by passing a file name , i.e. full path of the file (e.g. C://filename.txt ) or by passing a ‘File’ object or by passing a ‘FileDescriptor’ . In this example, I will show you by passing a ‘File’ object to the constructor.

To read bytes from the file, we can use ‘read()’ method on the ‘FileDescriptor’ object. It will read the byte and return an ‘int’ value. Keep reading the value till the return ‘int’ becomes ‘-1’ . ‘-1’ means that all bytes are completed reading. To print the value, first convert the ‘int’ to ‘char’ and print it. That’s it.

In this example, we are also getting the number of remaining bytes of the file at start using ‘available()’ method. This method returns an estimate of remaining bytes that can be read from the input stream. Let’s take a look into the program :

 * Copyright (C) 2017 codevscolor
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

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

 * Example class
public class ExampleClass {

    //utility method to print a string
    static void print(String value) {

     * Mehtod to read a file using FileInputStream
     * @throws IOException : This exception is thrown if any problem is found on closing the stream
    private static void readFile() throws IOException {

        //create a File object by passing the file name to its constructor
        File file = new File("C://file.txt");

        //create a FileInputStream variable with initial value as null
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(file);

            //print the number of bytes to read for this stream
            print("Approx " + fileInputStream.available() + "bytes" + " to read");

            int i = 0; //this int value will hold the reading byte

            while ((i = fileInputStream.read()) != -1) {
                //convert the reading byte to char and print the value
                System.out.print((char) i);

        } catch (FileNotFoundException e) {
        } catch (IOException e1) {
        } finally {
            if (fileInputStream != null) {

    public static void main(String[] args) {
        try {
        } catch (IOException e) {


Above program will print the contents of file “file.txt” . ‘readFile()’ method is called from the ‘main’ method to read the contents.

Categories: java

Leave a Reply

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