How do I read text file content line by line using commons-io?

Category: commons.io, viewed: 23850 time(s).
package org.kodejava.sample.commons.io;

import org.apache.commons.io.FileUtils;

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

public class ReadFileToListSample
{
    public static void main(String[] args)
    {
        // Create a file object of sample.txt
        File file = new File("sample.txt");

        try
        {
            // Here we read a file, sample.txt, using FileUtils
            // class of commons-io-1.3.1. Using FileUtils.readLines()
            // we can read file content line by line and return
            // the result as a List of string.
            List<String> contents = FileUtils.readLines(file);

            // Iterate the result to print each line of the file.
            for (String line : contents)
            {
                System.out.println(line);
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats