1

Is there a way to continue reading from the CSV after a cell processor exception is thrown? I'm attempting to take all of the good data from a file, do what I need with it, and report back to a user what, if any, bad lines are present. For example, if I have a file with just two columns (name, active). Name is just a string, and active is a boolean. Neither are optional in my list of CellProcessors. If a single row has null for the active column, I'd like to keep processing the lines afterwards.

Name, Active
Bob, 0
John, 1
Mary, 
Harry, 1

In this situation, Line 4 Mary will fail. I want to keep reading through to line 5. All examples show reading with while loops until EoF and I'm not seeing a way to get the number of lines present so I can use a for loop with internal try/catch blocks.

Curtis Snowden
  • 407
  • 1
  • 4
  • 20

1 Answers1

2

Did some testing myself and I came up with a workaround. It's certainly not ideal. I hope that the tryRead or a version of hasNext mentioned in a Git question last month is implemented, but this will work until then.

    boolean failed = false;
    do {
        try {
            failed = false;
            while( (myClassBean= beanReader.read(MyClass.class, processors)) != null ) {
                //Process the bean as normal
            }
        }
        catch (SuperCsvCellProcessorException sccpe) {
            //process the exception
            failed = true;
        }
   } while (failed);

The bean reader doesn't seem to have any problem maintaining its place in the csv, so all subsequent iterations of the do-while continue reading from the next line, if any. When EoF is reached, both loops terminate as desired.

Curtis Snowden
  • 407
  • 1
  • 4
  • 20