Apache Commons IO – LineIterator


Apache Commons IO – LineIterator



”;


LineIterator provides a flexible way to work with a line-based file.

Class Declaration

Following is the declaration for org.apache.commons.io.LineIterator Class −


public class LineIterator
   extends Object implements Iterator<String>, Closeable

Example of LineIterator Class

Here is the input file we need to parse −


Welcome to TutorialsPoint. Simply Easy Learning.
Learn web technologies,
prepare exams,
code online,
all at one place.

IOTester.java


import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingLineIterator();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingLineIterator() throws IOException {
      //get the file object
      File file = FileUtils.getFile("input.txt");
      try(LineIterator lineIterator = FileUtils.lineIterator(file)) {
         System.out.println("Contents of input.txt");
         while(lineIterator.hasNext()) {
            System.out.println(lineIterator.next());
         }
      }
   }
}

Output

It will print the following result −


Contents of input.txt
Welcome to TutorialsPoint. Simply Easy Learning.
Learn web technologies,
prepare exams,
code online,
all at one place.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *