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

Ads
 
Tutorials   
What are the prerequisites for learning Java? How to replace all occurrences of a string in JavaScript? How to convert a stack trace to a string? Example code of reading file line by line in Java with Apache Commons IO library How to store unique values in Java using Set? java.nio ShortBuffer Example How to use Java Path class? Java NIO Package Tutorial How to limit object creation in Java? Java 11 String lines function example to split it in lines Top upcoming features of JDK 11 Java/JDK 11 Tutorial How to install JDK 10 in Ubuntu 18.04? Install JDK 10 on Windows Java 10 features Processing large file in Java Learning Java for non programmers Why You Should Learn Java in 2018? Installing JDK 9 on Windows 10 How to convert binary to decimal in Java? How to add two numbers in Java? What is Applet in Java with Example? What is nested class in Java with Example? Exception handling in Java Programming Examples What is Constructor Overloading in Java? How to display image in Swing? What is Java programming used for? How to create custom exception in Java? How to set CLASSPATH in Java? What is Multithreading in Java? IO Streams in Java with Examples How to convert String Date to Timestamp in Java? Best Java Tutorial for Beginners Read user input in Java using Scanner Java Programming Examples Read file line by line in Java 8 What are the benefits of learning Java Programming? A brief history and future of Java programming language Importance of Java for Mobile Application Development How To Become A Good Software Developer?

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.

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.

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:

Ads

Ads