CSVParser implements Iterable<CSVRecord>, Closeable { public Iterator<CSVRecord> iterator() { return new Iterator<CSVRecord>() { private CSVRecord current; private CSVRecord getNextRecord() { try { return CSVParser.this.nextRecord(); } catch (final IOException e) { throw new RuntimeException(e); } } public boolean hasNext() { if (CSVParser.this.isClosed()) { return false; } if (this.current == null) { this.current = this.getNextRecord(); } return this.current != null; } public CSVRecord next() { if (CSVParser.this.isClosed()) { throw new NoSuchElementException("CSVParser has been closed"); } CSVRecord next = this.current; this.current = null; if (next == null) { next = this.getNextRecord(); if (next == null) { throw new NoSuchElementException("No more CSV records available"); } } return next; } public void remove() { throw new UnsupportedOperationException(); } }; } CSVParser(final Reader reader, final CSVFormat format); static CSVParser parse(File file, final CSVFormat format); static CSVParser parse(String string, final CSVFormat format); static CSVParser parse(URL url, Charset charset, final CSVFormat format); void close(); long getCurrentLineNumber(); Map<String, Integer> getHeaderMap(); long getRecordNumber(); List<CSVRecord> getRecords(); boolean isClosed(); Iterator<CSVRecord> iterator();  }
@Test public void testIterator() throws Exception { final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z"); final Iterator<CSVRecord> iterator = CSVFormat.DEFAULT.parse(in).iterator(); assertTrue(iterator.hasNext()); try { iterator.remove(); fail("expected UnsupportedOperationException"); } catch (final UnsupportedOperationException expected) { } assertArrayEquals(new String[]{"a", "b", "c"}, iterator.next().values()); assertArrayEquals(new String[]{"1", "2", "3"}, iterator.next().values()); assertTrue(iterator.hasNext()); assertTrue(iterator.hasNext()); assertTrue(iterator.hasNext()); assertArrayEquals(new String[]{"x", "y", "z"}, iterator.next().values()); assertFalse(iterator.hasNext()); try { iterator.next(); fail("NoSuchElementException expected"); } catch (final NoSuchElementException e) { } }