[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1104
  • Last Modified:

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

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
0
gudii9
Asked:
gudii9
  • 2
2 Solutions
 
CEHJCommented:
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

http://technojeeves.com/joomla/index.php/free/121-reading-a-file-into-a-list-of-string-in-java-7-and-above
0
 
ManishLeadCommented:
http://www.exampledepot.com/egs/java.io/readlinesfromfile.html
You can use split method of string to get | separated values.
0
 
CEHJCommented:
:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now