读取CSV文件
以下示例使用了CsvDozerBeanReader(我们刚刚发布的新阅读器,该阅读器使用Dozer进行了具有深度映射和基于索引的映射支持的Bean映射)-它基于我们网站上的示例。如果你不需要推土机功能(或者只需要一个简单的独立依赖项),则可以使用它CsvBeanReader(请参见此代码示例)。
CSV文件示例
这是一个示例CSV文件,代表对调查的答复。它有一个标题和3行数据,所有行都有8列。
age,consentGiven,questionNo1,answer1,questionNo2,answer2,questionNo3,answer318,Y,1,Twelve,2,Albert Einstein,3,Big Bang Theory,Y,1,Thirteen,2,Nikola Tesla,3,Stargate42,N,1,,2,Carl Sagan,3,Star Wars
定义从CSV到POJO的映射
CSV的每一行都会被读入SurveyResponse类,各自有一个列表答案秒。为了使映射起作用,你的类应为有效的Javabeans(即具有默认的no-arg构造函数,并为每个字段定义了getter / setter)。
在Super CSV中,你可以使用简单的String数组定义映射-数组的每个元素都对应于CSV文件中的一列。
与CsvDozerBeanMapper你一起可以使用:
- 简单字段映射(例如firstName)
- 深度映射(例如address.country.pre)
- 索引映射(例如middleNames[1]-数组或集合的从零开始的索引)
- 深度+索引映射(例如person.middleNames[1])
以下是此示例的字段映射-它使用了这些的组合:
private static final String[] FIELD_MAPPING = new String[] { "age", // simple field mapping (like for CsvBeanReader) "consentGiven", // as above "answers[0].questionNo", // indexed (first element) + deep mapping "answers[0].answer", "answers[1].questionNo", // indexed (second element) + deep mapping "answers[1].answer", "answers[2].questionNo", "answers[2].answer" };Conversion and Validation
Super CSV具有有用的单元处理器库,可用于将字符串从CSV文件转换为其他数据类型(例如,日期,整数),或进行约束验证(例如,强制/可选,正则表达式匹配,范围检查) 。
使用单元处理器完全是可选的 -没有它们,CSV的每一列都将是一个字符串,因此每个字段也必须是一个字符串。
以下是该示例的单元处理器配置。与字段映射一样,数组中的每个元素都代表一个CSV列。它演示了单元处理器如何将CSV数据转换为你字段的数据类型,以及如何将它们链接在一起。
final CellProcessor[] processors = new CellProcessor[] { new Optional(new ParseInt()), // age new ParseBool(), // consent new ParseInt(), // questionNo 1 new Optional(), // answer 1 new ParseInt(), // questionNo 2 new Optional(), // answer 2 new ParseInt(), // questionNo 3 new Optional() // answer 3};Reading
使用Super CSV进行读取非常灵活:你可以自己
Reader提供文件(这样你就可以从文件,类路径,zip文件等中读取文件),并且可以通过首选项(其中有许多前置定义的配置,可满足大多数使用情况)。
下面的代码是不言自明的。
- 创建读者(使用Reader和的首选项)
- (可选)阅读标题
- 配置bean映射
- 继续打电话,read()直到收到null(文件结尾)
- 关闭阅读器
码:
ICsvDozerBeanReader beanReader = null;try { beanReader = new CsvDozerBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE); beanReader.getHeader(true); // ignore the header beanReader.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING); SurveyResponse surveyResponse; while( (surveyResponse = beanReader.read(SurveyResponse.class, processors)) != null ) { System.out.println( String.format("lineNo=%s, rowNo=%s, surveyResponse=%s", beanReader.getLineNumber(), beanReader.getRowNumber(), surveyResponse)); }} finally { if( beanReader != null ) { beanReader.close(); }}输出:
lineNo=2, rowNo=2, surveyResponse=SurveyResponse [age=18, consentGiven=true, answers=[Answer [questionNo=1, answer=Twelve], Answer [questionNo=2, answer=Albert Einstein], Answer [questionNo=3, answer=Big Bang Theory]]]lineNo=3, rowNo=3, surveyResponse=SurveyResponse [age=null, consentGiven=true, answers=[Answer [questionNo=1, answer=Thirteen], Answer [questionNo=2, answer=Nikola Tesla], Answer [questionNo=3, answer=Stargate]]]lineNo=4, rowNo=4, surveyResponse=SurveyResponse [age=42, consentGiven=false, answers=[Answer [questionNo=1, answer=null], Answer [questionNo=2, answer=Carl Sagan], Answer [questionNo=3, answer=Star Wars]]]



