uniVocity parsers to handle two files

302 views Asked by At

I am using uniVocity in order to parse two different files. For every line of the first file I need to iterate through file 2 in order to make some comparisons.

    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);

Do I need to use different CsvParserSettings for the two parsers, or is there some other way to define rowProcessors?

Also how can I read the files line by line in order to perform the operations I need in each line?

1

There are 1 answers

0
Jeronimo Backes On BEST ANSWER

You can use the same settings, but will need new rowProcessor for each parser if you are going to run both parsers at the same time.

RowListProcessor anotherRowProcessor = new RowListProcessor();
settings.setRowProcessor(anotherRowProcessor); //using the same settings object here
CsvParser file2Parser = new CsvParser(settings);

However, from what you described it seems you'd be fine by not using a row processor and just iterate over the rows produced by each parser. In this case just get rid of the row processors, and do this:

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();

Hope this helps.