How to read big file line by line in java?

This programs shows you how to read big file line by line in java?

Learn how to write a program in java for reading big text file line by line

In this tutorial I will explain you how you can read big file line by line without using large memory. With the help of the program discussed here you will be able to read big size such as 5GB, 10GB or more. Many times you may have to read the big file in your java program. For example you have to process some legacy data and add it to your new database system.

Example discussed here is very useful in reading big file line by line in Java.

In this tutorial we will use and classes of package. These two classed will help us in making java program which reads the big file line by line.

About class

This class is used to read character files easily in Java program. Constructor takes file name as parameter.

About class

This class is used to efficiently  read characters, arrays, and lines from input stream. Its constructor takes input stream as input. Important methods of  class is:

read() - It reads  a character from the input stream.

read(char[] cbuf, int off, int len) - This method reads characters into a portion of an array.

readLine() - This method is used to read a complete line. It returns string line.

Following is complete example of java program that reads big text file line by line and then prints on console.

	This example code reads the big txt file libne by line


public class  ReadBigFileLineByLine
	public static void main(String[] args) 
			//Big file to read
			String fileName = "MyBigFile.txt";

			//Create FileReader object
			FileReader fileReader = new FileReader(fileName);

			//Instantiate BufferReader class
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			String oneLine;
			//Read file line by line
			while ((oneLine = bufferedReader.readLine()) != null) {
			   //Prints the content on console
			   //Write your own code here for processing the data

			//Close the buffer reader

		}catch(Exception e){
			System.out.println("Error while reading file:" + e.getMessage());

Share on Google+Share on Google+

How to read big file line by line in java?

Posted on: January 5, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: How to read big file line by line in java?  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 6, 2013

What libeartnig knowledge. Give me liberty or give me death.