11/*
22 * Java CSV is a stream based library for reading and writing
33 * CSV and other delimited data.
4- *
4+ *
55 * Copyright (C) Bruce Dunwiddie bruce@csvreader.com
66 *
77 * This library is free software; you can redistribute it and/or
2020 */
2121package main .com .csvreader ;
2222
23+ import java .io .BufferedWriter ;
2324import java .io .FileOutputStream ;
2425import java .io .IOException ;
2526import java .io .OutputStream ;
2627import java .io .OutputStreamWriter ;
27- import java .io .BufferedWriter ;
2828import java .io .Writer ;
2929import java .nio .charset .Charset ;
3030
3333 */
3434public class CsvWriter {
3535 private Writer outputStream = null ;
36-
36+
3737 private String fileName = null ;
3838
3939 private boolean firstColumn = true ;
@@ -48,7 +48,7 @@ public class CsvWriter {
4848 private boolean initialized = false ;
4949
5050 private boolean closed = false ;
51-
51+
5252 private String systemRecordDelimiter = System .getProperty ("line.separator" );
5353
5454 /**
@@ -155,7 +155,7 @@ public void setDelimiter(char delimiter) {
155155 userSettings .Delimiter = delimiter ;
156156 }
157157
158- public char getRecordDelimiter () {
158+ public String getRecordDelimiter () {
159159 return userSettings .RecordDelimiter ;
160160 }
161161
@@ -164,10 +164,33 @@ public char getRecordDelimiter() {
164164 *
165165 * @param recordDelimiter
166166 * The character to use as the record delimiter. Default is
167- * combination of standard end of line characters for Windows,
168- * Unix, or Mac.
167+ * the operative system native line separator.
168+ *
169+ * <p>Note this method does not allow you to set the Windows
170+ * native line separator as it is composed of 2 characters:
171+ * {@code \r\n}. If you need to set this separator (e.g.: if you
172+ * are in a Unix based operative system and need to output for
173+ * Windows) then you will have to use
174+ * {@link #setRecordDelimiter(String)}.</p>
175+ *
176+ * <p>This method was kept for compatibility with previous
177+ * versions.</p>
169178 */
170179 public void setRecordDelimiter (char recordDelimiter ) {
180+ setRecordDelimiter (String .valueOf (recordDelimiter ));
181+ }
182+
183+ /**
184+ * Sets the character to use as the record delimiter.
185+ *
186+ * @param recordDelimiter
187+ * The {@code String} to use as the record delimiter. Default is
188+ * the operative system native line separator.
189+ *
190+ * </p>This method was added in version 2.2 and allows the
191+ * delimiter to be a {@code String}.</p>
192+ */
193+ public void setRecordDelimiter (String recordDelimiter ) {
171194 useCustomRecordDelimiter = true ;
172195 userSettings .RecordDelimiter = recordDelimiter ;
173196 }
@@ -287,13 +310,13 @@ public void write(String content, boolean preserveSpaces)
287310 || (!useCustomRecordDelimiter && (content
288311 .indexOf (Letters .LF ) > -1 || content
289312 .indexOf (Letters .CR ) > -1 ))
290- || (useCustomRecordDelimiter && content
291- .indexOf (userSettings .RecordDelimiter ) > -1 )
292- || (firstColumn && content .length () > 0 && content
293- .charAt (0 ) == userSettings .Comment ) ||
294- // check for empty first column, which if on its own line must
295- // be qualified or the line will be skipped
296- (firstColumn && content .length () == 0 ))) {
313+ || (useCustomRecordDelimiter && content
314+ .indexOf (userSettings .RecordDelimiter ) > -1 )
315+ || (firstColumn && content .length () > 0 && content
316+ .charAt (0 ) == userSettings .Comment ) ||
317+ // check for empty first column, which if on its own line must
318+ // be qualified or the line will be skipped
319+ (firstColumn && content .length () == 0 ))) {
297320 textQualify = true ;
298321 }
299322
@@ -334,8 +357,8 @@ public void write(String content, boolean preserveSpaces)
334357 + Letters .BACKSLASH + userSettings .Delimiter );
335358
336359 if (useCustomRecordDelimiter ) {
337- content = replace (content , "" + userSettings .RecordDelimiter ,
338- "" + Letters .BACKSLASH + userSettings .RecordDelimiter );
360+ content = replace (content , userSettings .RecordDelimiter ,
361+ Letters .BACKSLASH + userSettings .RecordDelimiter );
339362 } else {
340363 content = replace (content , "" + Letters .CR , ""
341364 + Letters .BACKSLASH + Letters .CR );
@@ -391,7 +414,7 @@ public void writeComment(String commentText) throws IOException {
391414 } else {
392415 outputStream .write (systemRecordDelimiter );
393416 }
394-
417+
395418 firstColumn = true ;
396419 }
397420
@@ -412,8 +435,8 @@ public void writeComment(String commentText) throws IOException {
412435 public void writeRecord (String [] values , boolean preserveSpaces )
413436 throws IOException {
414437 if (values != null && values .length > 0 ) {
415- for (int i = 0 ; i < values . length ; i ++ ) {
416- write (values [ i ] , preserveSpaces );
438+ for (String value : values ) {
439+ write (value , preserveSpaces );
417440 }
418441
419442 endRecord ();
@@ -474,7 +497,7 @@ private void checkInit() throws IOException {
474497 * be written to the underlying device.
475498 * @exception IOException
476499 * Thrown if an error occurs while writing data to the
477- * destination stream.
500+ * destination stream.
478501 */
479502 public void flush () throws IOException {
480503 outputStream .flush ();
@@ -520,7 +543,7 @@ private void close(boolean closing) {
520543 private void checkClosed () throws IOException {
521544 if (closed ) {
522545 throw new IOException (
523- "This instance of the CsvWriter class has already been closed." );
546+ "This instance of the CsvWriter class has already been closed." );
524547 }
525548 }
526549
@@ -549,6 +572,7 @@ private class Letters {
549572 public static final char BACKSLASH = '\\' ;
550573
551574 public static final char NULL = '\0' ;
575+
552576 }
553577
554578 private class UserSettings {
@@ -560,7 +584,7 @@ private class UserSettings {
560584
561585 public char Delimiter ;
562586
563- public char RecordDelimiter ;
587+ public String RecordDelimiter ;
564588
565589 public char Comment ;
566590
@@ -572,7 +596,7 @@ public UserSettings() {
572596 TextQualifier = Letters .QUOTE ;
573597 UseTextQualifier = true ;
574598 Delimiter = Letters .COMMA ;
575- RecordDelimiter = Letters .NULL ;
599+ RecordDelimiter = String . valueOf ( Letters .NULL ) ;
576600 Comment = Letters .POUND ;
577601 EscapeMode = ESCAPE_MODE_DOUBLED ;
578602 ForceQualifier = false ;
0 commit comments