Java Program to Read Content From File Using BufferedReader

You are currently viewing Java Program to Read Content From File Using BufferedReader

Java program to read content from file using BufferedReader

// Java program to read content from file 
// using BufferedReader

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

public class ExBufferedReader {
  public static void main(String args[]) {
    final String fileName = "file2.txt";

    try {
      File objFile = new File(fileName);
      if (objFile.exists() == false) {
        System.out.println("File does not exist!!!");
        System.exit(0);
      }

      //reading content from file
      String text;
      int val;

      FileReader objFR = new FileReader(objFile.getAbsoluteFile());
      BufferedReader objBR = new BufferedReader(objFR);
      //read text from file
      System.out.println("Content of the file is: ");
      while ((text = objBR.readLine()) != null) {
        System.out.println(text);
      }

      objBR.close();
    } catch (Exception Ex) {
      System.out.println("Exception : " + Ex.toString());
    }
  }
}

Output

    
Content of the file is: 
Computer is an Electronic Device.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply