Search code examples
javanullopencsv

How to differentiate between empty string and null with OpenCSV


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?


Solution

  • 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) {
           // ...
       }
    }