12

This is my code:

CSVReader reader = new CSVReader(isReader);
while ((col = reader.readNext()) != null) {
   String c1 = col[1];
}

this is my csv file:

"a","","c"
"1",,"3"

Is there a way I can differentiate between null and ""? OpenCSV seems to treat everything as non-null String. Can I tell it to treat empty fields as null?

Gavriel
  • 18,880
  • 12
  • 68
  • 105

4 Answers4

9

Adding to @Gavriel's answer, starting from OpenCSV 3.6 (maybe earlier), I found that strictQuotes doesn't help with returning null anymore.

CSVReaderBuilder has withFieldAsNull() for this purpose.

CSVReader csvReader = new CSVReaderBuilder(csvFileReader)
    .withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS)
    .build();
Cary Li
  • 241
  • 1
  • 5
  • 9
  • I believe this must be the correct answer, since most of the reader constructors are Deprecated. By the way the indicator has BOTH option as well which considers both empty seperators and empty quotes as null. – Olgun Kaya Dec 24 '19 at 06:14
  • 1
    works for me only when set "withFieldAsNull" on CSVParserBuilder , not on CSVReaderBuilder , first create CSVParserBuilder , and with it create CSVReaderBuilder like this - CSVReaderBuilder(new CSVParserBuilder(...)) - (open csv 5.0) – lukass77 Mar 01 '20 at 10:27
  • is there a way to do it by column? – Krusty the Clown Aug 11 '23 at 18:17
5

with open csv 5.0

CSVParser csvParser = new CSVParserBuilder()
                        .withSeparator(",")
                        .withQuoteChar('"')
                        .withFieldAsNull(CSVReaderNullFieldIndicator.BOTH)
                        .build();

                csvReader = new CSVReaderBuilder(new InputStreamReader(...))
                        .withCSVParser(csvParser)
                        .build();
lukass77
  • 376
  • 5
  • 9
4

It is not possible. There is no difference between an empty string and the java specific null in the CSV format's philosophy. The null is an empty reference in the java object memory model. In CSV there aren't any references, but only empty values.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • 1
    There is. In the above csv example I would expect opencsv to notice the difference between ,, and ,"",. I would be OK with the default being convert everything to non-null String, but I would expect to have an optional parameter where I could override the default behavior and let opencsv know that whenever it sees a certain string it should consider it as NULL. But in order for this to work it has to be inside the parser (I can't extend opencsv to add this IMHO) because in the level I get to the data it's already converted to "" – Gavriel Aug 14 '14 at 08:14
  • @Gavriel, according to `CSV` format, `,,` and `,"",` are same things. – Andremoniy Aug 14 '14 at 08:16
  • where is that standard? I thought there is no csv RFC yet – Gavriel Aug 14 '14 at 09:29
  • I don't see anything about empty fields there. – Gavriel Aug 14 '14 at 11:57
2

I found the solution: strictQuotes can be used to get nulls:

CSVReader reader = new CSVReader(isReader, ',', '"', true);
while ((col = reader.readNext()) != null) {
   String c1 = col[1];
   if (null != c1) {
       // ...
   }
}
Gavriel
  • 18,880
  • 12
  • 68
  • 105