@@ -83,6 +83,8 @@ public final class CSVPrinter implements Flushable, Closeable {
8383 /** True if we just began a new record. */
8484 private boolean newRecord = true ;
8585
86+ private long recordCount ;
87+
8688 /**
8789 * Creates a printer that will print values to the given stream following the CSVFormat.
8890 * <p>
@@ -140,6 +142,17 @@ public void close(final boolean flush) throws IOException {
140142 }
141143 }
142144
145+ /**
146+ * Outputs the record separator and increments the record count.
147+ *
148+ * @throws IOException
149+ * If an I/O error occurs
150+ */
151+ private synchronized void endOfRecord () throws IOException {
152+ println ();
153+ recordCount ++;
154+ }
155+
143156 /**
144157 * Flushes the underlying stream.
145158 *
@@ -162,6 +175,16 @@ public Appendable getOut() {
162175 return this .appendable ;
163176 }
164177
178+ /**
179+ * Gets the record count printed, this does not include comments or headers.
180+ *
181+ * @return the record count, this does not include comments or headers.
182+ * @since 1.13.0
183+ */
184+ public long getRecordCount () {
185+ return recordCount ;
186+ }
187+
165188 /**
166189 * Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
167190 *
@@ -235,7 +258,10 @@ public synchronized void printComment(final String comment) throws IOException {
235258 * @since 1.9.0
236259 */
237260 public synchronized void printHeaders (final ResultSet resultSet ) throws IOException , SQLException {
238- printRecord ((Object []) format .builder ().setHeader (resultSet ).build ().getHeader ());
261+ try (IOStream <String > stream = IOStream .of (format .builder ().setHeader (resultSet ).build ().getHeader ())) {
262+ stream .forEachOrdered (this ::print );
263+ }
264+ println ();
239265 }
240266
241267 /**
@@ -265,7 +291,7 @@ public synchronized void println() throws IOException {
265291 @ SuppressWarnings ("resource" )
266292 public synchronized void printRecord (final Iterable <?> values ) throws IOException {
267293 IOStream .of (values ).forEachOrdered (this ::print );
268- println ();
294+ endOfRecord ();
269295 }
270296
271297 /**
@@ -302,7 +328,7 @@ public void printRecord(final Object... values) throws IOException {
302328 @ SuppressWarnings ("resource" ) // caller closes.
303329 public synchronized void printRecord (final Stream <?> values ) throws IOException {
304330 IOStream .adapt (values ).forEachOrdered (this ::print );
305- println ();
331+ endOfRecord ();
306332 }
307333
308334 private void printRecordObject (final Object value ) throws IOException {
@@ -426,7 +452,7 @@ public void printRecords(final ResultSet resultSet) throws SQLException, IOExcep
426452 print (object );
427453 }
428454 }
429- println ();
455+ endOfRecord ();
430456 }
431457 }
432458
0 commit comments