Simple Java Code To Read A File Using BufferedReader

Hello, friends. I am here again with another simple Java example code. Are you trying to read the text of a file? If yes, then this post may give you the solution.

Java BufferedReader is a Java class which can be used to read the text from a character-based input stream. To use this you need to import Java.io.BufferedReader.

In this tutorial, I am going to give you the simple example code which shows you how to read texts from a text file or any other text supported file using BufferedReader Class in Java.

Java Code to read the file

At first, we are going to import the whole java.io package so that all the required java classes are available to use using the below code on top:

import java.io.*;

The above code contains BufferedReader and FileReader Class with lots of other classes. But in this example, we are just going to create an object of BufferedReader and FileReader classes.



Now let’s start writing inside our main method of Java. Use the throws Exception so that our code will run perfectly. The main method will look like below with throws Exception:

public static void main(String[] args) throws Exception {
      // required code will be here
  }

After that, we have to start writing the required code inside this main method.

At first, let’s take an integer  type variable:

int ch;

Now create a text file “myfile.txt” in the same directory where your java file exist. After that, in our main function create an object of FileReader class:

FileReader fr=new FileReader("myfile.txt");

I have put the below text inside my text file:

Hello World
This is a dummy text file
to read using Java

Now create an object of BufferedReader Class and use the object of FileReader Class that we have just created:

BufferedReader rdf=new BufferedReader(fr);

Now we have to run a while loop which will read the int value while it does not become -1 and then in the while loop print the text by converting the int into char:

 while((ch=rdf.read())!=-1) {
  System.out.print((char)ch);
}

We have use (char)ch to convert the ch integer type into char.

It is a good practice to close the object using the below code:

rdf.close();

 Here is the final and complete code

The complete code is given below:

import java.io.*;

public class ReadFile {
    public static void main(String[] args) throws Exception {
    int ch;
    FileReader fr=new FileReader("myfile.txt");
    BufferedReader rdf=new BufferedReader(fr);
    
    while((ch=rdf.read())!=-1) {
      System.out.print((char)ch);
    }
    rdf.close();
    }
}

It will return the result that is given below:

Hello World
This is a dummy text file
to read using Java

So we are able to read the text file successfully. We can also read any other text supported file with other types of file extension name. So how about this Java tutorial? I hope you have understood the code.

Leave a Reply

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