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

我想跳过第一行并使用第二行作为标题.

我正在使用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
}

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...