Package org.apache.commons.io
Class LineIterator
java.lang.Object
org.apache.commons.io.LineIterator
- All Implemented Interfaces:
- Closeable,- AutoCloseable,- Iterator<String>
An Iterator over the lines in a 
Reader.
 
 LineIterator holds a reference to an open Reader.
 When you have finished with the iterator you should close the reader
 to free internal resources. This can be done by closing the reader directly,
 or by calling the close() or closeQuietly(LineIterator)
 method on the iterator.
 
The recommended usage pattern is:
 LineIterator it = FileUtils.lineIterator(file, StandardCharsets.UTF_8.name());
 try {
   while (it.hasNext()) {
     String line = it.nextLine();
     // do something with line
   }
 } finally {
   it.close();
 }
 - Since:
- 1.2
- 
Constructor SummaryConstructorsConstructorDescriptionLineIterator(Reader reader) Constructs an iterator of the lines for aReader.
- 
Method SummaryModifier and TypeMethodDescriptionvoidclose()Closes the underlyingReader.static voidcloseQuietly(LineIterator iterator) Deprecated.As of 2.6 deprecated without replacement.booleanhasNext()Indicates whether theReaderhas more lines.protected booleanisValidLine(String line) Overridable method to validate each line that is returned.next()Returns the next line in the wrappedReader.nextLine()Deprecated.Usenext().voidremove()Unsupported.Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.IteratorforEachRemaining
- 
Constructor Details- 
LineIteratorConstructs an iterator of the lines for aReader.- Parameters:
- reader- the- Readerto read from, not null
- Throws:
- NullPointerException- if the reader is null
 
 
- 
- 
Method Details- 
closeQuietlyDeprecated.As of 2.6 deprecated without replacement. Please use the try-with-resources statement or handle suppressed exceptions manually.Closes aLineIteratorquietly.- Parameters:
- iterator- The iterator to close, or- null.
- See Also:
 
- 
closeCloses the underlyingReader. This method is useful if you only want to process the first few lines of a larger file. If you do not close the iterator then theReaderremains open. This method can safely be called multiple times.- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- Closeable
- Throws:
- IOException- if closing the underlying- Readerfails.
 
- 
hasNextIndicates whether theReaderhas more lines. If there is anIOExceptionthenclose()will be called on this instance.- Specified by:
- hasNextin interface- Iterator<String>
- Returns:
- trueif the Reader has more lines
- Throws:
- IllegalStateException- if an IO exception occurs
 
- 
isValidLineOverridable method to validate each line that is returned. This implementation always returns true.- Parameters:
- line- the line that is to be validated
- Returns:
- true if valid, false to remove from the iterator
 
- 
nextReturns the next line in the wrappedReader.- Specified by:
- nextin interface- Iterator<String>
- Returns:
- the next line from the input
- Throws:
- NoSuchElementException- if there is no line to return
 
- 
nextLineDeprecated.Usenext().Returns the next line in the wrappedReader.- Returns:
- the next line from the input
- Throws:
- NoSuchElementException- if there is no line to return
 
- 
removeUnsupported.- Specified by:
- removein interface- Iterator<String>
- Throws:
- UnsupportedOperationException- always
 
 
-