Apache common IO offers a great utility class. LineIterator is an iterator over a the lines in a Reader
File file = new File("inputFile,txt");
LineIterator lineIterator = null;
try
{
lineIterator = FileUtils.lineIterator(file);
while(lineIterator.hasNext())
{
String line = lineIterator.next();
// Process line
logger.info (line);
}
}
catch (IOException e)
{
// Handle exception
}
finally
{
LineIterator.closeQuietly(lineIterator);
}
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.