Skip to content

Non-standard control characters

The CSV specification (section 2.6. of RFC 4180) defines the following control characters:

  • Field separator: ,
  • Record delimiter: \r\n
  • Field enclosure: "

FastCSV uses these control characters by default. However, you can change them using the CsvReaderBuilder or CsvWriterBuilder class.

Using non-standard control characters for Read

In the following example, a CSV file with non-standard control characters is read using FastCSV.

ExampleCsvReaderWithNonStandardControlCharacters.java
package example;
import de.siegmar.fastcsv.reader.CsvReader;
/**
* Example for reading CSV data with control characters that are not the default/standard ones.
*/
public class ExampleCsvReaderWithNonStandardControlCharacters {
private static final String DATA = """
'foo';'bar'
'foo2';'bar2'
""";
public static void main(final String[] args) {
CsvReader.builder()
.fieldSeparator(';')
.quoteCharacter('\'')
.ofCsvRecord(DATA)
.forEach(System.out::println);
}
}

You also find this source code example in the FastCSV GitHub repository.

Using non-standard control characters for Write

In the following example, a CSV file with non-standard control characters is created using FastCSV.

ExampleCsvWriterWithNonStandardControlCharacters.java
package example;
import de.siegmar.fastcsv.writer.CsvWriter;
import de.siegmar.fastcsv.writer.LineDelimiter;
/**
* Example for writing CSV data with non-standard control characters.
*/
public class ExampleCsvWriterWithNonStandardControlCharacters {
public static void main(final String[] args) {
// The default configuration uses a comma as field separator,
// a double quote as quote character and
// a CRLF as line delimiter.
CsvWriter.builder()
.fieldSeparator(';')
.quoteCharacter('\'')
.lineDelimiter(LineDelimiter.LF)
.toConsole()
.writeRecord("header1", "header2")
.writeRecord("value1", "value;2");
}
}

You also find this source code example in the FastCSV GitHub repository.