📜  Apache Commons IO-LineIterator

📅  最后修改于: 2020-11-18 08:16:07             🧑  作者: Mango


提供了一种处理基于行的文件的灵活方式。

类声明

以下是org.apache.commons.io.LineIterator类的声明-

public class LineIterator
   extends Object implements Iterator, Closeable

LineIterator类的示例

这是我们需要解析的输入文件-

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

输出

它将打印以下结果。

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