How To Read File In Java with BufferedReader

Tutorial teaches you How To Read File In Java with BufferedReader?

How To Read File In Java with BufferedReader

Tutorial teaches you How To Read File In Java with BufferedReader?

How To Read File In Java with BufferedReader

How To Read File In Java with BufferedReader class - example code

This tutorial shows you how you can read file using BufferedReader class in your program. This tutorial is using the BufferedReader class for reading simple text file. The BufferedReader class provides added benefits of increasing the performance of the application.

Our program is simple program that reads a text file line by line.

Program prints the content of the file on console.

Here is the complete code of the program for reading file with BufferedReader class:

import java.io.*;
/**
* How To Read File In Java with BufferedReader class
*
*/
public class ReadUsingBufferedReader 
{
	public static void main(String[] args) 
	{
		System.out.println("Reading File with BufferedReader class");
		//Name of the file
		String fileName="test.text";
		try{
		
		//Create object of FileReader
		FileReader inputFile = new FileReader(fileName);
		
		//Instantiate the BufferedReader Class
		BufferedReader bufferReader = new BufferedReader(inputFile);

		//Variable to hold the one line data
		String line;
		
		// Read file line by line and print on the console
		while ((line = bufferReader.readLine()) != null)   {
			System.out.println("File Line is: " + line);
		}
		//Close the buffer reader
		bufferReader.close();
		}catch(Exception e){
			System.out.println("Error in reading file:" + e.getMessage());			
		}
	}
}