【发布时间】:2018-02-02 08:21:50
【问题描述】:
我想跳过第一行并使用第二行作为标题。
我正在使用 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 有什么区别?
【问题讨论】:
-
在将 fileReader 提供给解析器之前,您是否尝试过读取直到 newLine?
标签: java csv apache-commons-csv