Using Java FileReader to Read a File

Java FileReader in character streams used to read files in the same way as in FileOutputStream. Instead of bytes we use characters. The Java FileReader example below demonstrates it.



import java.io.FileReader;
import java.io.File;
class JavaFileReader
{
public static void main(String args[]) throws Exception
{

// Create File object for JavaFileReader.java
File f=new File("JavaFileReader.java");

//---------First way--------//

// Create FileReader object
FileReader fr=new FileReader(f);

// Create a char array of file length
char[] c=new char[(int)f.length()];

// Dump data into char array c
fr.read(c);

System.out.println("\n\nFirst way\n");

// Print the data
for(int i=0;i<c.length;i++)
{
System.out.print(c[i]);
}

// Close FileReader
fr.close();

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

// Create FileReader object
FileReader fr1=new FileReader(f);

// Create char array of file length
char[] c1=new char[(int)f.length()];

// Dump from 0 to end into c1
fr1.read(c1,0,c1.length);

System.out.println("\n\nSecond way\n");

for(int i=0;i<c1.length;i++)
{
// Print every character
System.out.print(c1[i]);
}

// Close FileReader
fr1.close();

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

// Create FileReader object
FileReader fr2=new FileReader(f);

// An integer whose initial value is 0
int k;

System.out.println("\n\nThird way\n");

// Read and store in k till end of file
while((k=fr2.read())!=-1)
{
// Print what is read
System.out.print((char)k);
}

// Close the FileReader
fr2.close();

}
}
why f.length(): As in the FileInputStream, there is no method like available() in the FileReader class. So, we have used the f.length() and to obtain it we need to create a java.io.File Object.

fr.read(c): This method dumps the entire data that the java.io.FileReader points into the char array 'c'.

fr.read(c1,0,c1.length): This method dumps the entire data that the java.io.FileReader points, into the char array 'c1' from the start position '0' till the end position 'c1.length' (nothing but the end of the file).

(k=fr2.read())!=-1: This method, reads each character from the file (JavaFileReader.java) at a time and store in an integer called 'k' whose initial value is '0' (because it is Java! :)) . After the value read is stored in 'k' , the System.out.print() method is called instead of println() (which prints in a new line) because we don't want each character in a new line instead we want the file as it is. This prints the value of 'k' ( to print the char, k is converted into char type).

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


First way

import java.io.FileReader;
import java.io.File;
class JavaFileReader
{
        public static void main(String args[]) throws Exception
        {

// Create File object for JavaFileReader.java
        File f=new File("JavaFileReader.java");

//---------First way--------//

// Create FileReader object
        FileReader fr=new FileReader(f);

// Create a char array of file length
        char[] c=new char[(int)f.length()];

// Dump data into char array c
        fr.read(c);

        System.out.println("\n\nFirst way\n");

        // Print the data
                for(int i=0;i<c.length;i++)
                {
                System.out.print(c[i]);
                }

        // Close FileReader
        fr.close();

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

        // Create FileReader object
        FileReader fr1=new FileReader(f);

        // Create char array of file length
        char[] c1=new char[(int)f.length()];

        // Dump from 0 to end into c1
        fr1.read(c1,0,c1.length);

        System.out.println("\n\nSecond way\n");

                for(int i=0;i<c1.length;i++)
                {
                // Print every character
                System.out.print(c1[i]);
                }

        // Close FileReader
        fr1.close();

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

// Create FileReader object
        FileReader fr2=new FileReader(f);

// An integer whose initial value is 0
        int k;

        System.out.println("\n\nThird way\n");

// Read and store in k till end of file
                while((k=fr2.read())!=-1)
                {
                // Print what is read
                System.out.print((char)k);
                }

        // Close the FileReader
        fr2.close();

        }
}

Second way

import java.io.FileReader;
import java.io.File;
class JavaFileReader
{
        public static void main(String args[]) throws Exception
        {

// Create File object for JavaFileReader.java
        File f=new File("JavaFileReader.java");

//---------First way--------//

// Create FileReader object
        FileReader fr=new FileReader(f);

// Create a char array of file length
        char[] c=new char[(int)f.length()];

// Dump data into char array c
        fr.read(c);

        System.out.println("\n\nFirst way\n");

        // Print the data
                for(int i=0;i<c.length;i++)
                {
                System.out.print(c[i]);
                }

        // Close FileReader
        fr.close();

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

        // Create FileReader object
        FileReader fr1=new FileReader(f);

        // Create char array of file length
        char[] c1=new char[(int)f.length()];

        // Dump from 0 to end into c1
        fr1.read(c1,0,c1.length);

        System.out.println("\n\nSecond way\n");

                for(int i=0;i<c1.length;i++)
                {
                // Print every character
                System.out.print(c1[i]);
                }

        // Close FileReader
        fr1.close();

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

// Create FileReader object
        FileReader fr2=new FileReader(f);

// An integer whose initial value is 0
        int k;

        System.out.println("\n\nThird way\n");

// Read and store in k till end of file
                while((k=fr2.read())!=-1)
                {
                // Print what is read
                System.out.print((char)k);
                }

        // Close the FileReader
        fr2.close();

        }
}

Third way

import java.io.FileReader;
import java.io.File;
class JavaFileReader
{
        public static void main(String args[]) throws Exception
        {

// Create File object for JavaFileReader.java
        File f=new File("JavaFileReader.java");

//---------First way--------//

// Create FileReader object
        FileReader fr=new FileReader(f);

// Create a char array of file length
        char[] c=new char[(int)f.length()];

// Dump data into char array c
        fr.read(c);

        System.out.println("\n\nFirst way\n");

        // Print the data
                for(int i=0;i<c.length;i++)
                {
                System.out.print(c[i]);
                }

        // Close FileReader
        fr.close();

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

        // Create FileReader object
        FileReader fr1=new FileReader(f);

        // Create char array of file length
        char[] c1=new char[(int)f.length()];

        // Dump from 0 to end into c1
        fr1.read(c1,0,c1.length);

        System.out.println("\n\nSecond way\n");

                for(int i=0;i<c1.length;i++)
                {
                // Print every character
                System.out.print(c1[i]);
                }

        // Close FileReader
        fr1.close();

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

// Create FileReader object
        FileReader fr2=new FileReader(f);

// An integer whose initial value is 0
        int k;

        System.out.println("\n\nThird way\n");

// Read and store in k till end of file
                while((k=fr2.read())!=-1)
                {
                // Print what is read
                System.out.print((char)k);
                }

        // Close the FileReader
        fr2.close();

        }
}