uniVocity 解析器处理两个文件

uniVocity parsers to handle two files

我正在使用 uniVocity 来解析两个不同的文件。对于第一个文件的每一行,我需要遍历文件 2 以便进行一些比较。

    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

另外,如何逐行读取文件以便在每一行中执行我需要的操作?

您可以使用相同的设置,但如果您要同时 运行 两个解析器,则每个解析器都需要新的 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();

希望对您有所帮助。