advertise with ongsono.com
 

Using Java FileReader To Read A File

Posted on Wednesday, 3 October 2012


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 {
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 {
// 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                {
                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                {
                // 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                {
                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                {
                // 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                {
                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                {
                // 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();

        }
}


Read more about Using Java FileReader to Read a File



Related read :

Xplore 3.77 file manager terbaik untuk hp java jar
Download X PLORE 3.77 jar file manager terbaik untuk hp java. X-PLORE 3.77 jar adalah file manager terbaik untuk HP java, Artikel sebelumnya : Kumpulan Web hosting gratis terbaik dan subdomain
doPost() in doGet() in Java Servlets - What's the difference?
doPost() in doGet() in ServletsTill now we have seen working with doPost() and doGet(). Now let us see how to write doPost() in doGet() before that lets us talk about them for some time. doPost()
Music Tag - mp3 File Tagger v1.0
Music Tag - mp3 File Tagger v1.0 - Keep your music files organized! mTag is ONLY of its kind app that allows you to edit mp3 file names and organize your Music library by empowering you to give
Customize Blogger Read More / Jump Breaks Links With image Buttons, Text And
Hi dear friend,after reading this tutorial youll be able to Customize you read more links in those ways:Change the text read more to another words.Add An Image / Button To Your Read More / Jump