Read Text File in Java using FileReader Class

Sharing is Caring

Previously I have posted about reading text file using FileInputStreamBufferedReader and Scanner Class. This post shows how to read text file in java using FileReader class.

This is a sample text file

Sample Text File - FileReader Class

and this is the java code to read text file using FileReader class

package client;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileFr {
    public ReadFileFr() {
        super();
    }

    public static void main(String[] args) {
        // Absolute path of file that you want to read
        FileReader fr=null;
        try {
            fr = new FileReader("D:/sampleFile.txt");
        } catch (FileNotFoundException e) {
            System.out.println(e);
        }

        int data;
        //Iterate over file content
        try {
            while ((data = fr.read()) != -1)
                System.out.print((char) data);
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}

 

Execute this java code

Output is

Read text file in java using FileReader

Cheers 🙂 Happy Learning

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

Leave a Reply

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