加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何跳过Java中csv的第一行?

发布时间:2020-12-14 06:05:02 所属栏目:Java 来源:网络整理
导读:我想跳过第一行并使用第二行作为标题. 我正在使用apache commons csv中的类来处理CSV文件. CSV文件的标题位于第二行,而不是第一行(包含坐标). 我的代码看起来像这样: static void processFile(final File file) { FileReader filereader = new FileReader(f
我想跳过第一行并使用第二行作为标题.

我正在使用apache commons csv中的类来处理CSV文件.

CSV文件的标题位于第二行,而不是第一行(包含坐标).

我的代码看起来像这样:

static void processFile(final File file) {
    FileReader filereader = new FileReader(file);
    final CSVFormat format = CSVFormat.DEFAULT.withDelimiter(';');
    CSVParser parser = new CSVParser(filereader,format);
    final List<CSVRecord> records = parser.getRecords();
    //stuff
}

我天真地想,

CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(;)

会解决问题,因为它与withFirstRowAsHeader不同,我认为它会检测到第一行不包含任何分号而不是记录.它没有.我试图跳过第一行(CSVFormat似乎认为是标题)

CSVFormat format = CSVFormat.DEFAULT.withSkipHeaderRecord().withFirstRecordAsHeader().withDelimiter(;);

但这也行不通.我能做什么? withFirstRowAsHeader和withFirstRecordAsHeader之间的区别是什么?

解决方法

在将阅读器传递给CSVParser之前,您可能需要阅读第一行:
static void processFile(final File file) {
    FileReader filereader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(filereader);
    bufferedReader.readLine();// try-catch omitted
    final CSVFormat format = CSVFormat.DEFAULT.withDelimiter(';');
    CSVParser parser = new CSVParser(bufferedReader,format);
    final List<CSVRecord> records = parser.getRecords();
    //stuff
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读