3 Ways to Read a File using FileInputStream in Java IO

Reading a file in Java IO (streams) with FileInputStream is much easier than what you think of. Here are 3 effective ways you can use to achieve it.The sample tutorial decides and simplifies doing the task.


import java.io.*;
class ReadAndPrintFile
{
public static void main(String args[]) throws Exception
{
//---------------------------First way-------------------------//
FileInputStream fin=new FileInputStream("ReadAndPrintFile.java");
// Create a byte array
byte[] b=new byte[fin.available()];
// Read data into the array
fin.read(b);
for(int i=0;i<b.length;i++)
{
System.out.print((char)b[i]);
}
//---------------------------Second way-------------------------//
int k=0;
FileInputStream fin1=new FileInputStream("ReadAndPrintFile.java");
System.out.println("\n\nSecond way\n");
// Read till the end of file
while((k=fin1.read())!=-1)
{
System.out.print((char)k);
}
//---------------------------Third way-------------------------//
FileInputStream fin2=new FileInputStream("ReadAndPrintFile.java");
byte b1[]=new byte[fin2.available()];
fin2.read(b1,0,b1.length);
System.out.println("\n\nThird way\n");
for(int i=0;i<b1.length;i++)
{
System.out.print((char)b1[i]);
}

// Close the document
fin.close();
fin1.close();
fin2.close();
}
}
fin.read(b): Dump the entire data that is read into the byte array which is stored in RAM.
(k=fin.read())!=-1: Reading each byte from the file starting from the index '0' till the end of the file. If  end of the file is reached the fin.read() will return -1.
fin.read(b1,0,b1.length): The first parameter is the byte array in which the data should be dumped into, the second parameter is the start-index of the file, that is from which the dumping should start, the last parameter is the index. Here i'm saying to dump till the end of the file i.e. the entire file is dumped as i am specifying '0' as start and 'b1.length' as last index which is equal to fin.available().

-------------------------------------
Output
-------------------------------------

import java.io.*;
class ReadAndPrintFile
{
        public static void main(String args[]) throws Exception
        {

//---------------------------First way-------------------------//
        FileInputStream fin=new FileInputStream("ReadAndPrintFile.java");

        // Create a byte array
        byte[] b=new byte[fin.available()];

        // Read data into the array
        fin.read(b);

                for(int i=0;i<b.length;i++)
                {
                System.out.print((char)b[i]);
                }

//---------------------------Second way-------------------------//

        int k=0;

        FileInputStream fin1=new FileInputStream("ReadAndPrintFile.java");
        System.out.println("\n\nSecond way\n");
                // Read till the end of file
                while((k=fin1.read())!=-1)
                {
                        System.out.print((char)k);
                }

//---------------------------Third way-------------------------//

        FileInputStream fin2=new FileInputStream("ReadAndPrintFile.java");
        byte b1[]=new byte[fin2.available()];
        fin2.read(b1,0,b1.length);

        System.out.println("\n\nThird way\n");
                for(int i=0;i<b1.length;i++)
                {
                System.out.print((char)b1[i]);
                }


        // Close the document
        fin.close();
        fin1.close();
        fin2.close();
        }
}

Second way

import java.io.*;
class ReadAndPrintFile
{
        public static void main(String args[]) throws Exception
        {

//---------------------------First way-------------------------//
        FileInputStream fin=new FileInputStream("ReadAndPrintFile.java");

        // Create a byte array
        byte[] b=new byte[fin.available()];

        // Read data into the array
        fin.read(b);

                for(int i=0;i<b.length;i++)
                {
                System.out.print((char)b[i]);
                }

//---------------------------Second way-------------------------//

        int k=0;

        FileInputStream fin1=new FileInputStream("ReadAndPrintFile.java");
        System.out.println("\n\nSecond way\n");
                // Read till the end of file
                while((k=fin1.read())!=-1)
                {
                        System.out.print((char)k);
                }

//---------------------------Third way-------------------------//

        FileInputStream fin2=new FileInputStream("ReadAndPrintFile.java");
        byte b1[]=new byte[fin2.available()];
        fin2.read(b1,0,b1.length);

        System.out.println("\n\nThird way\n");
                for(int i=0;i<b1.length;i++)
                {
                System.out.print((char)b1[i]);
                }


        // Close the document
        fin.close();
        fin1.close();
        fin2.close();
        }
}

Third way

import java.io.*;
class ReadAndPrintFile
{
        public static void main(String args[]) throws Exception
        {

//---------------------------First way-------------------------//
        FileInputStream fin=new FileInputStream("ReadAndPrintFile.java");

        // Create a byte array
        byte[] b=new byte[fin.available()];

        // Read data into the array
        fin.read(b);

                for(int i=0;i<b.length;i++)
                {
                System.out.print((char)b[i]);
                }

//---------------------------Second way-------------------------//

        int k=0;

        FileInputStream fin1=new FileInputStream("ReadAndPrintFile.java");
        System.out.println("\n\nSecond way\n");
                // Read till the end of file
                while((k=fin1.read())!=-1)
                {
                        System.out.print((char)k);
                }

//---------------------------Third way-------------------------//

        FileInputStream fin2=new FileInputStream("ReadAndPrintFile.java");
        byte b1[]=new byte[fin2.available()];
        fin2.read(b1,0,b1.length);

        System.out.println("\n\nThird way\n");
                for(int i=0;i<b1.length;i++)
                {
                System.out.print((char)b1[i]);
                }


        // Close the document
        fin.close();
        fin1.close();
        fin2.close();
        }
}