Parsing SuperCsv/beanreader encounters exception on first line and return NULL

StackOverflow https://stackoverflow.com/questions/15220818

  •  18-03-2022
  •  | 
  •  

Question

I try to parse a csv file using Supercsv. I use some "catch exception" to eliminate row with wrong structure or data.

All lines are processed through a do / while loop. But if the first line causes an exception (regular expression...) the return of the read() method is null whichc causes the stop of the while loop.

How can I avoid this by keeping the bad lines' elimination ?

Here is the code :

CsvPreference LSP_PREFERENCE = new CsvPreference.Builder(' ', ';', "\n").build();
beanReader = new CsvBeanReader(new InputStreamReader(new FileInputStream(fileToTreat), "ISO-8859-1"), LSP_PREFERENCE);
            CsvPreference LSP_PREFERENCE = new CsvPreference.Builder(' ', ';', "\n").build();
            beanReader = new CsvBeanReader(new InputStreamReader(new FileInputStream(fileToTreat), "ISO-8859-1"), LSP_PREFERENCE);
treatedOk = true;


ucdr = null;

do {
    try {
         ucdr = beanReader.read(ConvergeDataRow.class, headers, processors);
         if (ucdr != null) {
           rdata.add(ucdr);   // contains list of row data.
         }
     } catch (SuperCsvConstraintViolationException ex) {
          logger.log(Level.SEVERE, "NON CORRECT VALUE ENCOUNTERD ON ROW "+beanReader.getRowNumber(), ex);
          treatedOk = false;

     } catch (SuperCsvCellProcessorException ex){
         logger.log(Level.SEVERE, "PARSER EXCEPTION ON ROW "+beanReader.getRowNumber(), ex);
         treatedOk = false;
     } catch (org.supercsv.exception.SuperCsvException ex){
          logger.log(Level.SEVERE, "ERROR ON ROW "+beanReader.getRowNumber(), ex);
                    treatedOk = false;
     }

} while (ucdr != null);treatedOk = true;

A doom solution would be to force ucdr to an other value of null. But there is certainly a better way...

Could you help me?

Était-ce utile?

La solution

The problem is caused by the fact that ucdr (the container for a parsed line) can be null either at the end of the file, or due to a bad line in your file. As your code handles all exceptions caused by a bad line, there's no way to know if ucdr is null because file end was reached or whether a bad line was encountered. What I propose is to use another condition for the loop, one that will be false only if ucdr is null because the end of the file was reached, like this:

boolean continueLooping = true;
do {
    try {
         ucdr = beanReader.read(ConvergeDataRow.class, headers, processors);
         if (ucdr != null) {
           rdata.add(ucdr);   // contains list of row data.
         }
         // this line will be skipped when an exception is thrown during 
         // parsing, but not if the line is successfully handled or on end of
         // file .
         continueLooping = ucdr != null;
     } catch (SuperCsvConstraintViolationException ex) {
          logger.log(Level.SEVERE, "NON CORRECT VALUE ENCOUNTERD ON ROW "+beanReader.getRowNumber(), ex);
          treatedOk = false;

     } catch (SuperCsvCellProcessorException ex){
         logger.log(Level.SEVERE, "PARSER EXCEPTION ON ROW "+beanReader.getRowNumber(), ex);
         treatedOk = false;
     } catch (org.supercsv.exception.SuperCsvException ex){
          logger.log(Level.SEVERE, "ERROR ON ROW "+beanReader.getRowNumber(), ex);
                    treatedOk = false;
     }

} while (continueLooping);
treatedOk = true;

Note: treatedOk being set to true outside the loop looks a bit suspicious...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top