2016-12-29 50 views
1

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

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'

另外我怎样才能逐行读取文件,以执行我需要在每一行中的操作?

回答

1

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

希望这有助于。