14

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?

4 Answers 4

11

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();
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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)
is there a way to do it by column?
7

with open csv 5.0

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

                csvReader = new CSVReaderBuilder(new InputStreamReader(...))
                        .withCSVParser(csvParser)
                        .build();

1 Comment

shoud use CSVReaderNullFieldIndicator.EMPTY_SEPARATORS instead of CSVReaderNullFieldIndicator.BOTH, then the result will be a,,c and 1,null,3
3

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.

4 Comments

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, according to CSV format, ,, and ,"", are same things.
where is that standard? I thought there is no csv RFC yet
I don't see anything about empty fields there.
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) {
       // ...
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.