Here you can find the source of readFileLineByLine(final File filePath)
public static Iterator<String> readFileLineByLine(final File filePath) throws java.io.IOException
//package com.java2s; //License from project: Apache License import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Iterator; import java.util.zip.GZIPInputStream; public class Main { public static Iterator<String> readFileLineByLine(final File filePath) throws java.io.IOException { return new Iterator<String>() { // Open the file that is the first // command line parameter InputStream fstream = new FileInputStream(filePath); {//from w w w . j a va2s . c om if (filePath.getName().endsWith(".gz")) { // Automatically unzip zipped files. fstream = new GZIPInputStream(fstream); } } // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String next = br.readLine(); @Override public boolean hasNext() { final boolean result = (next != null); if (!result) { try { br.close(); } catch (final IOException e) { throw new RuntimeException(e); } } return result; } @Override public String next() { final String result = next; try { next = br.readLine(); } catch (final IOException e) { throw new RuntimeException(e); } return result; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } }