@@ -31,6 +31,7 @@ public class CSVFormat implements Serializable {
3131
3232 /** According to RFC 4180, line breaks are delimited by CRLF */
3333 private static final String CRLF = "\r \n " ;
34+
3435 private final char delimiter ;
3536 private final char encapsulator ;
3637 private final char commentStart ;
@@ -39,7 +40,8 @@ public class CSVFormat implements Serializable {
3940 private final boolean trailingSpacesIgnored ;
4041 private final boolean unicodeEscapesInterpreted ;
4142 private final boolean emptyLinesIgnored ;
42- private final String lineSeparator ; // for output
43+ private final String lineSeparator ; // for outputs
44+ private final String [] header ;
4345
4446
4547 /**
@@ -51,7 +53,7 @@ public class CSVFormat implements Serializable {
5153 static final char DISABLED = '\ufffe' ;
5254
5355 /** Standard comma separated format as defined by <a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>. */
54- public static final CSVFormat DEFAULT = new CSVFormat (',' , '"' , DISABLED , DISABLED , true , true , false , true , CRLF );
56+ public static final CSVFormat DEFAULT = new CSVFormat (',' , '"' , DISABLED , DISABLED , true , true , false , true , CRLF , null );
5557
5658 /**
5759 * Excel file format (using a comma as the value delimiter).
@@ -64,10 +66,10 @@ public class CSVFormat implements Serializable {
6466 *
6567 * <pre>CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');</pre>
6668 */
67- public static final CSVFormat EXCEL = new CSVFormat (',' , '"' , DISABLED , DISABLED , false , false , false , false , CRLF );
69+ public static final CSVFormat EXCEL = new CSVFormat (',' , '"' , DISABLED , DISABLED , false , false , false , false , CRLF , null );
6870
6971 /** Tab-delimited format, with quote; leading and trailing spaces ignored. */
70- public static final CSVFormat TDF = new CSVFormat ('\t' , '"' , DISABLED , DISABLED , true , true , false , true , CRLF );
72+ public static final CSVFormat TDF = new CSVFormat ('\t' , '"' , DISABLED , DISABLED , true , true , false , true , CRLF , null );
7173
7274 /**
7375 * Default MySQL format used by the <tt>SELECT INTO OUTFILE</tt> and
@@ -77,7 +79,7 @@ public class CSVFormat implements Serializable {
7779 *
7880 * @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html">http://dev.mysql.com/doc/refman/5.1/en/load-data.html</a>
7981 */
80- public static final CSVFormat MYSQL = new CSVFormat ('\t' , DISABLED , DISABLED , '\\' , false , false , false , false , "\n " );
82+ public static final CSVFormat MYSQL = new CSVFormat ('\t' , DISABLED , DISABLED , '\\' , false , false , false , false , "\n " , null );
8183
8284
8385 /**
@@ -92,6 +94,7 @@ public class CSVFormat implements Serializable {
9294 * @param unicodeEscapesInterpreted <tt>true</tt> when unicode escapes should be interpreted
9395 * @param emptyLinesIgnored <tt>true</tt> when the parser should skip emtpy lines
9496 * @param lineSeparator the line separator to use for output
97+ * @param header the header
9598 */
9699 CSVFormat (
97100 char delimiter ,
@@ -102,7 +105,8 @@ public class CSVFormat implements Serializable {
102105 boolean trailingSpacesIgnored ,
103106 boolean unicodeEscapesInterpreted ,
104107 boolean emptyLinesIgnored ,
105- String lineSeparator ) {
108+ String lineSeparator ,
109+ String [] header ) {
106110 this .delimiter = delimiter ;
107111 this .encapsulator = encapsulator ;
108112 this .commentStart = commentStart ;
@@ -112,6 +116,7 @@ public class CSVFormat implements Serializable {
112116 this .unicodeEscapesInterpreted = unicodeEscapesInterpreted ;
113117 this .emptyLinesIgnored = emptyLinesIgnored ;
114118 this .lineSeparator = lineSeparator ;
119+ this .header = header ;
115120 }
116121
117122 /**
@@ -171,7 +176,7 @@ public CSVFormat withDelimiter(char delimiter) {
171176 throw new IllegalArgumentException ("The delimiter cannot be a line break" );
172177 }
173178
174- return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator );
179+ return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator , header );
175180 }
176181
177182 /**
@@ -195,7 +200,7 @@ public CSVFormat withEncapsulator(char encapsulator) {
195200 throw new IllegalArgumentException ("The encapsulator cannot be a line break" );
196201 }
197202
198- return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator );
203+ return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator , header );
199204 }
200205
201206 boolean isEncapsulating () {
@@ -223,7 +228,7 @@ public CSVFormat withCommentStart(char commentStart) {
223228 throw new IllegalArgumentException ("The comment start character cannot be a line break" );
224229 }
225230
226- return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator );
231+ return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator , header );
227232 }
228233
229234 /**
@@ -256,7 +261,7 @@ public CSVFormat withEscape(char escape) {
256261 throw new IllegalArgumentException ("The escape character cannot be a line break" );
257262 }
258263
259- return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator );
264+ return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator , header );
260265 }
261266
262267 boolean isEscaping () {
@@ -280,7 +285,7 @@ public boolean isLeadingSpacesIgnored() {
280285 * @return A copy of this format with the specified left trimming behavior.
281286 */
282287 public CSVFormat withLeadingSpacesIgnored (boolean leadingSpacesIgnored ) {
283- return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator );
288+ return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator , header );
284289 }
285290
286291 /**
@@ -300,7 +305,7 @@ public boolean isTrailingSpacesIgnored() {
300305 * @return A copy of this format with the specified right trimming behavior.
301306 */
302307 public CSVFormat withTrailingSpacesIgnored (boolean trailingSpacesIgnored ) {
303- return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator );
308+ return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator , header );
304309 }
305310
306311 /**
@@ -311,7 +316,7 @@ public CSVFormat withTrailingSpacesIgnored(boolean trailingSpacesIgnored) {
311316 * @return A copy of this format with the specified trimming behavior.
312317 */
313318 public CSVFormat withSurroundingSpacesIgnored (boolean surroundingSpacesIgnored ) {
314- return new CSVFormat (delimiter , encapsulator , commentStart , escape , surroundingSpacesIgnored , surroundingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator );
319+ return new CSVFormat (delimiter , encapsulator , commentStart , escape , surroundingSpacesIgnored , surroundingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator , header );
315320 }
316321
317322 /**
@@ -332,7 +337,7 @@ public boolean isUnicodeEscapesInterpreted() {
332337 * @return A copy of this format with the specified unicode escaping behavior.
333338 */
334339 public CSVFormat withUnicodeEscapesInterpreted (boolean unicodeEscapesInterpreted ) {
335- return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator );
340+ return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator , header );
336341 }
337342
338343 /**
@@ -352,7 +357,7 @@ public boolean isEmptyLinesIgnored() {
352357 * @return A copy of this format with the specified empty line skipping behavior.
353358 */
354359 public CSVFormat withEmptyLinesIgnored (boolean emptyLinesIgnored ) {
355- return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator );
360+ return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator , header );
356361 }
357362
358363 /**
@@ -372,15 +377,37 @@ public String getLineSeparator() {
372377 * @return A copy of this format using the specified output line separator
373378 */
374379 public CSVFormat withLineSeparator (String lineSeparator ) {
375- return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator );
380+ return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator , header );
381+ }
382+
383+ String [] getHeader () {
384+ return header ;
385+ }
386+
387+ /**
388+ * Returns a copy of this format using the specified header. The header can
389+ * either be parsed automatically from the input file with:
390+ *
391+ * <pre>CSVFormat format = CSVFormat.DEFAULT.withHeader();</pre>
392+ *
393+ * or specified manually with:
394+ *
395+ * <pre>CSVFormat format = CSVFormat.DEFAULT.withHeader("name", "email", "phone");</pre>
396+ *
397+ * @param header the header, <tt>null</tt> if disabled, empty if parsed automatically, user specified otherwise.
398+ *
399+ * @return A copy of this format using the specified header
400+ */
401+ public CSVFormat withHeader (String ... header ) {
402+ return new CSVFormat (delimiter , encapsulator , commentStart , escape , leadingSpacesIgnored , trailingSpacesIgnored , unicodeEscapesInterpreted , emptyLinesIgnored , lineSeparator , header );
376403 }
377404
378405 /**
379406 * Parses the specified content.
380407 *
381408 * @param in the input stream
382409 */
383- public Iterable <String [] > parse (Reader in ) {
410+ public Iterable <CSVRecord > parse (Reader in ) throws IOException {
384411 return new CSVParser (in , this );
385412 }
386413
0 commit comments