Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Commit 3a20344

Browse files
committed
[CSV-220] Add API
org.apache.commons.csv.CSVFormat.withSystemRecordSeparator().
1 parent 34262e8 commit 3a20344

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<action issue="CSV-217" type="add" dev="ggregory" due-to="Korolyov Alexei">Add autoFlush option for CsvPrinter. PR #24.</action>
4343
<action issue="CSV-219" type="fix" dev="ggregory" due-to="Zhang Hongda">The behavior of quote char using is not similar as Excel does when the first string contains CJK char(s).</action>
4444
<action issue="CSV-172" type="fix" dev="ggregory" due-to="Andrew Pennebaker">Don't quote cells just because they have UTF-8 encoded characters.</action>
45+
<action issue="CSV-220" type="add" dev="ggregory" due-to="Gary Gregory">Add API org.apache.commons.csv.CSVFormat.withSystemRecordSeparator().</action>
4546
</release>
4647
<release version="1.5" date="2017-09-03" description="Feature and bug fix release">
4748
<action issue="CSV-203" type="fix" dev="ggregory" due-to="Richard Wheeldon, Kai Paroth">withNullString value is printed without quotes when QuoteMode.ALL is specified; add QuoteMode.ALL_NON_NULL. PR #17.</action>

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,22 @@ public CSVFormat withSkipHeaderRecord(final boolean skipHeaderRecord) {
19251925
skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
19261926
}
19271927

1928+
/**
1929+
* Returns a new {@code CSVFormat} with the record separator of the format set to the operating system's line
1930+
* separator string, typically CR+LF on Windows and LF on Linux.
1931+
*
1932+
* <p>
1933+
* <strong>Note:</strong> This setting is only used during printing and does not affect parsing. Parsing currently
1934+
* only works for inputs with '\n', '\r' and "\r\n"
1935+
* </p>
1936+
*
1937+
* @return A new CSVFormat that is equal to this but with the operating system's line separator stringr
1938+
* @since 1.6
1939+
*/
1940+
public CSVFormat withSystemRecordSeparator() {
1941+
return withRecordSeparator(System.getProperty("line.separator"));
1942+
}
1943+
19281944
/**
19291945
* Returns a new {@code CSVFormat} to add a trailing delimiter.
19301946
*

src/test/java/org/apache/commons/csv/CSVFormatTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ public void testSerialization() throws Exception {
640640
public void testToString() {
641641

642642
final CSVFormat cSVFormat = CSVFormat.POSTGRESQL_TEXT;
643-
final String string = cSVFormat.INFORMIX_UNLOAD.toString();
643+
final String string = CSVFormat.INFORMIX_UNLOAD.toString();
644644

645645
assertEquals("Delimiter=<|> Escape=<\\> QuoteChar=<\"> RecordSeparator=<\n> EmptyLines:ignored SkipHeaderRecord:false", string);
646646

@@ -1090,4 +1090,10 @@ public void testWithRecordSeparatorLF() throws Exception {
10901090
assertEquals(String.valueOf(LF), formatWithRecordSeparator.getRecordSeparator());
10911091
}
10921092

1093+
@Test
1094+
public void testWithSystemRecordSeparator() throws Exception {
1095+
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withSystemRecordSeparator();
1096+
assertEquals(System.getProperty("line.separator"), formatWithRecordSeparator.getRecordSeparator());
1097+
}
1098+
10931099
}

0 commit comments

Comments
 (0)