Skip first line using Open CSV reader

不问归期 提交于 2019-12-01 02:35:35
Abhijeet Doshi

This constructor of CSVReader class will skip 1st line of the csv while reading the file.

CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 1);
Sinsanator

I found this question and response helpful, I'd like to expand on Christophe Roussy's comment. In the latest opencsv (2.3 as of this writing) The actual line of code is:

new CSVReader( new StringReader(csvText), CSVParser.DEFAULT_SEPARATOR,
               CSVParser.DEFAULT_QUOTE_CHARACTER, 1);

Note it uses CSVParser instead of CSVReader.

At least since version 3.8 you can use the CSVReaderBuilder and set it to skip the first line.

Example:

CSVReader reader = new CSVReaderBuilder(inputStreamReader)
                .withFieldAsNull(CSVReaderNullFieldIndicator.EMPTY_SEPARATORS)
                // Skip the header
                .withSkipLines(1)
                .build();

with latest version opencsv version use -

CSVReader csvReader = new CSVReaderBuilder(new FileReader("book.csv")).withSkipLines(1).build()

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!