Read Text File in Java using BufferedReader

Sharing is Caring

Previously I have posted about reading text file using FileInputStream and this post shows how to read text file in java using BufferedReader class.

This is a sample text file

Sample Text File - BufferedReader

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

package client;

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

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

    public static void main(String[] args)  {
        // Absolute path of file that you want to read
        File f = new File("D:/sampleFile.txt");

        BufferedReader bfr=null;
        try {
            bfr = new BufferedReader(new FileReader(f));
        } catch (FileNotFoundException e) {
        }

        String data;
        //Iterate over file content
        try {
            while ((data = bfr.readLine()) != null)
                System.out.println(data);
        } catch (IOException e) {
        }
    }
}

 

Execute this java code

Output is

Read text file in java using BufferedReader

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 *