How do I get a List of the lines in a File using Commons IO?
Author: Deron Eriksson
Description: This Java tutorial describes an easy way to get a List of the lines in a File.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)

The ApacheSW Commons IOS library has a very useful tool for allowing you to get a List of the lines of a File, the readLines() method of the FileUtils class. The ReadListOfLinesFromFile class demonstrates this.

package test;

import java.util.List;


public class ReadListOfLinesFromFile {

	public static void main(String[] args) {
		try {
			File file = new File("test.txt");
			List<String> lines = FileUtils.readLines(file);

			for (String line : lines) {
				System.out.println("line:" + line);

		} catch (IOException e) {

The test.txt file is shown here:


this is
a test
of commons io

Executing ReadListOfLinesFromFile with test.txt generates the following console output:

line:this is
line:a test
line:of commons io