Java 处理两个文件的uniVocity解析器
我使用uniVocity来解析两个不同的文件。对于第一个文件的每一行,我需要遍历文件2,以便进行一些比较Java 处理两个文件的uniVocity解析器,java,csv,univocity,Java,Csv,Univocity,我使用uniVocity来解析两个不同的文件。对于第一个文件的每一行,我需要遍历文件2,以便进行一些比较 RowListProcessor rowProcessor = new RowListProcessor(); CsvParserSettings settings = new CsvParserSettings(); settings.setRowProcessor(rowProcessor); settings.setLineSeparatorDetec
RowListProcessor rowProcessor = new RowListProcessor();
CsvParserSettings settings = new CsvParserSettings();
settings.setRowProcessor(rowProcessor);
settings.setLineSeparatorDetectionEnabled(true);
settings.setHeaderExtractionEnabled(true);
settings.setSkipEmptyLines(true);
CsvParser file1Parser = new CsvParser(settings);
CsvParser file2Parser = new CsvParser(settings);
我是否需要为这两个解析器使用不同的CsvParserSettings
,或者是否有其他方法来定义rowProcessor
s
另外,我如何逐行读取文件以执行每行中需要的操作?您可以使用相同的设置,但如果要同时运行两个解析器,则每个解析器都需要新的
rowProcessor
RowListProcessor anotherRowProcessor = new RowListProcessor();
settings.setRowProcessor(anotherRowProcessor); //using the same settings object here
CsvParser file2Parser = new CsvParser(settings);
然而,从您描述的情况来看,不使用行处理器,只需迭代每个解析器生成的行就可以了。在这种情况下,只需去掉行处理器,然后执行以下操作:
CsvParser file1Parser=new CsvParser(settings);
CsvParser file2Parser=new CsvParser(settings);
file1Parser.beginParsing(file1);
file2Parser.beginParsing(file2);
String[] rowOfParser1;
String[] rowOfParser2;
while((rowOfParser1 = file1Parser.parseNext()) != null){
rowOfParser2 = file2Parser.parseNext();
//do whatever you need to do with the rows.
}
//only need to call this if you are not reading both inputs entirely
file1Parser.stopParsing();
file2Parser.stopParsing();
希望这有帮助