Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCSV quoting null values

Tags:

java

csv

opencsv

Using the OpenCSV library, calling StatefulBeanToCsv.write() my null values are being wrapped in quotes.

Example:

String[] columns = new String[] {
    "Col1",
    "Col2",
    "Col3"
};

ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setColumnMapping(columns);

Writer writer = new FileWriter(outputFilePath);
StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer)
        .withMappingStrategy(strat)
        .build();
beanToCsv.write(items);
writer.close();

will produce:

1,"",3

When I expect:

"1",,"3"

I have not set quotes to all fields via .withApplyQuotesToAll(true).

If I do use .withApplyQuotesToAll(true), I end up with

"1","","3"

At one point, it appears the library the opposite of this:

OpenCSV CSVWriter does not add quote character for null element

How can I null values written as a blank/empty value, rather than an empty string?

like image 828
Brett Avatar asked Sep 06 '25 03:09

Brett


1 Answers

It looks like the method that you mentioned not calling actually takes a boolean. Have you tried the following?

.withApplyQuotesToAll(false)
like image 84
Jared Stewart Avatar answered Sep 07 '25 22:09

Jared Stewart