Home Java Javafile How To Read File In Java with BufferedReader
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

How To Read File In Java with BufferedReader

Advertisement
Tutorial teaches you 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());			
		}
	}
}
Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 20, 2013

Ask Questions?    Discuss: How To Read File In Java with BufferedReader  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com