I've never used Spring Batch before, I have a csv file which I need to read all columns with a specific model but I have many exceptions.
The csv file has 8 fields. if all the five fields are there, the file processing works well. but if by mistake, if 8 fields are not entered, like below:
c1;c2;C3;C4;C5;C6;C7;C8 // IT WORKS c1;C3;C7 // NOT WORKS : Program crashes
The error is: Caused by: org.springframework.batch.item.file.transform.IncorrectTokenCountException: Incorrect number of tokens found in record: expected 3 actual 8.
My code :
@Bean
public Step Step2(StepBuilderFactory stepBuilders) throws IOException {
System.out.println("cecStep2");
return stepBuilders.get("fileReject")
.<CSCivique, String>chunk(100)
.reader(reader())
.processor(processor2FileReject())
.writer(writer2())
.build();
}
@Bean
public FlatFileItemReader<CSCivique> reader() throws IOException{
try {
return new FlatFileItemReaderBuilder<CSCivique>().name("personItemReader")
.resource(new ClassPathResource(confFile ()))
.delimited()
.delimiter(";")
.names(new String[] { "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8" })
.targetType(CSCivique.class)
.strict(false)
.build();
}catch(Exception e) {
System.out.println("----------- Exception reader() --------------");
return null ;
}
}
I want to save lines in file reject like : c1;C3;C7: please check the number of fields
Thank you.