【发布时间】:2017-06-28 10:06:34
【问题描述】:
如果标题总是预先知道,下面的代码会解析 CSV 记录,我们可以为 FILE_HEADER_MAPPING 声明数组值。
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING);
FileReader fileReader = new FileReader("file");
CSVParser csvFileParser = new CSVParser(fileReader, csvFileFormat);
Iterable<CSVRecord> records = csvFileParser.getRecords();
但是如何为每个 csv 文件的标题不同的 CSV 文件创建 CSVParser。
我不知道要使用格式创建的 csv 文件的标题
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING);
我想为每个可能的 csv 标头提供一个 csv 解析器。 请帮我解决这个问题。
【问题讨论】:
-
我猜这是 apache commons CSV 解析器?
-
是的,它是 apache csv 解析器。
-
你读过标题自动检测吗?这也许可以解决您的问题:commons.apache.org/proper/commons-csv/…
-
但是.. 我们在哪个版本中有这个方法
-
遇到编译器错误.. for Iterable
csvRecords = CSVFormat.RFC4180.withFirstRowAsHeader().parse(reader);
标签: java csv parsing apache-commons