public class TextFile implements Iterable<String>{ private BufferedReader br; public TextFile(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); } @Override public Iterator<String> iterator() { return new Iterator<String>() { @Override public boolean hasNext() { try { br.mark(1); if (br.read() < 0) { return false; } br.reset(); return true; } catch (IOException e) { return false; } } @Override public String next() { try { return br.readLine(); } catch (IOException e) { return null; } } @Override public void remove() { throw new UnsupportedOperationException(); } }; } }