Example code of reading file line by line in Java with Apache Commons IO library

How to read file line by line in Java with apache commons IO library classes? Learn to use file handling classes of Apache Commons IO library.

Example code of reading file line by line in Java with Apache Commons IO library

How to read file line by line in Java with apache commons IO library classes? Learn to use file handling classes of Apache Commons IO library.

Example code of reading file line by line in Java with Apache Commons IO library

Step by step example of reading file line by line using Apache Commons IO package class

There are many ways of reading test file in Java with the help of many different available packages and classes in Java library. Based on different use cases developer must select the appropriate method of reading file in Java. If file size is small and can fit into the memory then its better to read file in memory as String or List. But if you are working on batch processing job and you have to read huge text file sequentially then its better to read file line by line. Reading file line by line is the best and efficient method for processing a big file in Java.

In this example program we will use the Apache commons IO FileUtils class for reading a text file one line at a time. This example program will read one line at a time and process the data. After processing line data it will read next next line until end of file. This is fast and efficient method of processing big file in Java program by reading only one line at a time.

Step 1: Create a project in Eclipse and convert it to maven project.

First of all you can create a new project in Eclipse or you can use any Eclipse and maven project for running this example code. You can to add following dependency code into pom.xml file:

<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.6</version>
</dependency>

Above maven code will add Apache commons IO package jar file into your project. Maven is easy way to add dependency in Java based projects. Apache Commons IO package contains classes for IO functionality in Java. It helps developers in making programs that performs IO operations.

Read file line by line in Java

These classes comes with the easy to use functions which can be used by developers quickly. In this example we will use FileUtils class for reading file text line by line.

Step 2: Create a test file to test application

The next step is to create a text file for testing example. You can also use any big text file if you have with you to test program performance on the big file. Here in this example we are creating data.txt file and adding following lines in it:

One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten

This is simple text file with 10 lines of data. Create this file in the project root as shown below:

Java Read file with apache commons io libraries

Step 3: Write Java program to read file one line at a time

Create a new class in Eclipse with the name ApacheCommonsReadFile (ApacheCommonsReadFile.java) and add following code:

package net.roseindia;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class ApacheCommonsReadFile {

public static void main(String[] args) {
	try {
	File f = new File("data.txt");
	BufferedReader b = new BufferedReader(new FileReader(f));
	System.out.println("Apache commons read file line by line example");
	List<String> lines = FileUtils.readLines(f, "UTF-8");
	for (String line : lines) {
		System.out.println(line);
	}
	} catch (IOException e) {
		e.printStackTrace();
	}
}
}

In this example FileUtils class of Apache Commons API is used to read one line at a time from a text file. The readLines() method of FileUtils class takes BufferedReader  as object.

The readLines() method of this class is used to read a file line by line into List of Strings. This is function is efficient can can be used to read text file of any size.

If you run the example it will read data from data.txt file and print it on console line by line. You can write data processing logic to meet your business requirement. You can process data one line a a time.

Here are more example of reading file in Java: