read text file content line by line using commons-io with delimitter

Posted on 2012-08-20
Last Modified: 2012-08-23
I was trying to run folowing example from following link
to read and the contents of a text file whose data is separated by delimitters say |

import; import;
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. 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) {                
                  } catch (IOException e) {          

When I try to run getting compilation error. Do I need to download and add any JAR to run this example. Where I need to place the text file. If I place it under c:\test folder can I modify code as

File file = new File("C:\test\sample.txt");

How do I separate delimtted code in text file as in attachment. My code is not recognizing the org.apache.common, FileUtils, contents etc. please advise.Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
Question by:gudii9
    LVL 86

    Accepted Solution

    You need to add the commons io jar to your project's build path together with its dependencies.
    If you're using Java 1.7 you can do the following with no extra libraries
    LVL 11

    Assisted Solution

    You can use split method of string to get | separated values.
    LVL 86

    Expert Comment


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    powerN  challenge 3 36
    How Complex Is This Java Course ? 9 52
    mapShare challenge 13 42
    Systems talking to each other 5 73
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now