如何使用 OpenCSV 区分空字符串和 null [英] How to differentiate between empty string and null with OpenCSV

查看:150
本文介绍了如何使用 OpenCSV 区分空字符串和 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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

这是我的 csv 文件:

this is my csv file:

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

有什么方法可以区分 null 和 ""?OpenCSV 似乎将所有内容都视为非空字符串.我可以告诉它将空字段视为空字段吗?

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?

推荐答案

我找到了解决方案:strictQuotes 可用于获取空值:

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

这篇关于如何使用 OpenCSV 区分空字符串和 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆