这是我的代码:
CSVReader reader = new CSVReader(isReader);
while ((col = reader.readNext()) != null) {
String c1 = col[1];
}
Run Code Online (Sandbox Code Playgroud)
这是我的csv文件:
"a","","c"
"1",,"3"
Run Code Online (Sandbox Code Playgroud)
有没有办法区分null和“”?OpenCSV似乎将所有内容都视为非null字符串。我可以告诉它将空字段视为null吗?
添加到@Gavriel 的答案,从 OpenCSV 3.6(可能更早)开始,我发现 strictQuotes 不再有助于返回 null。
为此,CSVReaderBuilder 具有 withFieldAsNull() 。
CSVReader csvReader = new CSVReaderBuilder(csvFileReader)
.withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS)
.build();
Run Code Online (Sandbox Code Playgroud)
打开 csv 5.0
CSVParser csvParser = new CSVParserBuilder()
.withSeparator(",")
.withQuoteChar('"')
.withFieldAsNull(CSVReaderNullFieldIndicator.BOTH)
.build();
csvReader = new CSVReaderBuilder(new InputStreamReader(...))
.withCSVParser(csvParser)
.build();
Run Code Online (Sandbox Code Playgroud)
我找到了解决方案: strictQuotes 可用于获取空值:
CSVReader reader = new CSVReader(isReader, ',', '"', true);
while ((col = reader.readNext()) != null) {
String c1 = col[1];
if (null != c1) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8580 次 |
| 最近记录: |