We help IT Professionals succeed at work.
Get Started

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

gudii9
gudii9 asked
on
1,192 Views
Last Modified: 2012-08-23
I was trying to run folowing example from following link

http://www.kodejava.org/examples/53.html
to read and the contents of a text file whose data is separated by delimitters say |


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. 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();        
                  }    
            }
      }


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
report.txt
ApacheCommons.JPG
Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 2 Answers and 3 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE