Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing the read line with FlatFileItemReader

I'm reading a csv file with FlatFileItemReader and using a FieldSetMapper to map it to a domain object.

In case the mapping fails, I would like to have the original line read by the FlatFileItemReader at my disposal so I could write it to another csv file.

My original idea was to use some listener to push the read string to StepContext and then a SkipListener to fetch the string to write it to another file. But I can't find a way to catch the original line read by the FlatFileItemReader.

Suggestions?

like image 418
vertti Avatar asked Nov 07 '25 18:11

vertti


1 Answers

Implementing the onReadError(Exception) method of the ItemReadListener interface does exactly what you need.

public void onReadError(Exception e) {
    if(e instanceof FlatFileParseException) {
        FlatFileParseException ffpe = (FlatFileParseException) e;
        ffpe.getLineNumber(); //The line number error occured
        ffpe.getInput();//Text value of the unparsed line
    }
}
like image 138
Serkan Arıkuşu Avatar answered Nov 09 '25 08:11

Serkan Arıkuşu