Top Description Fields Constructors Methods
org.apache.commons.io

public Class LineIterator

extends Object
implements Iterator
Class Inheritance
All Implemented Interfaces
java.util.Iterator
Imports
java.io.BufferedReader, .IOException, .Reader, java.util.Iterator, .NoSuchElementException

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, "UTF-8");
try {
  while (it.hasNext()) {
    String line = it.nextLine();
    /// do something with line
  }
} finally {
  LineIterator.closeQuietly(iterator);
}
Authors
Niall Pemberton, Stephen Colebourne, Sandy McArthur
Since
Commons IO 1.2
Version
$Id: LineIterator.java 437567 2006-08-28 06:39:07Z bayard $

Field Summary

Modifier and TypeField and Description
private final BufferedReader
bufferedReader

The reader that is being read.

private String
cachedLine

The current line.

private boolean
finished

A flag indicating if the iterator has been fully read.

Constructor Summary

AccessConstructor and Description
public
LineIterator(final Reader
the Reader to read from, not null
reader
)

Constructs an iterator of the lines for a Reader.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Closes the underlying Reader quietly.

public static void
closeQuietly(LineIterator
the iterator to close
iterator
)

Closes the iterator, handling null and ignoring exceptions.

public boolean

Returns:

true if the Reader has more lines
hasNext
()

Implements java.util.Iterator.hasNext.

Indicates whether the Reader has more lines.
protected boolean

Returns:

true if valid, false to remove from the iterator
isValidLine
(String
the line that is to be validated
line
)

Overridable method to validate each line that is returned.

public Object

Returns:

the next line from the input
next
()

Implements java.util.Iterator.next.

Returns the next line in the wrapped Reader.
public String

Returns:

the next line from the input
nextLine
()

Returns the next line in the wrapped Reader.

public void
remove()

Overrides default java.util.Iterator.remove.

Unsupported.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferedReaderback to summary
private final BufferedReader bufferedReader

The reader that is being read.

cachedLineback to summary
private String cachedLine

The current line.

finishedback to summary
private boolean finished

A flag indicating if the iterator has been fully read.

Constructor Detail

LineIteratorback to summary
public LineIterator(final Reader reader) throws IllegalArgumentException

Constructs an iterator of the lines for a Reader.

Parameters
reader:Reader

the Reader to read from, not null

Exceptions
IllegalArgumentException:
if the reader is null

Method Detail

closeback to summary
public void close()

Closes the underlying Reader quietly. 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 the Reader remains open. This method can safely be called multiple times.

closeQuietlyback to summary
public static void closeQuietly(LineIterator iterator)

Closes the iterator, handling null and ignoring exceptions.

Parameters
iterator:LineIterator

the iterator to close

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Indicates whether the Reader has more lines. If there is an IOException then close() will be called on this instance.

Returns:boolean

true if the Reader has more lines

Exceptions
IllegalStateException:
if an IO exception occurs
isValidLineback to summary
protected boolean isValidLine(String line)

Overridable method to validate each line that is returned.

Parameters
line:String

the line that is to be validated

Returns:boolean

true if valid, false to remove from the iterator

nextback to summary
public Object next()

Implements java.util.Iterator.next.

Returns the next line in the wrapped Reader.

Returns:Object

the next line from the input

Exceptions
NoSuchElementException:
if there is no line to return
nextLineback to summary
public String nextLine()

Returns the next line in the wrapped Reader.

Returns:String

the next line from the input

Exceptions
NoSuchElementException:
if there is no line to return
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Unsupported.

Exceptions
UnsupportedOperationException:
always