I have flatfile, .csv file It has 5 five fields. if all the five fields are there, the file processing works well. but if by mistake, if 5 fields are not entered, like below
d,e,f,g,h
a,b,c
d,e,f,g,h
the program crashes. I want to continue reading to the next line.
The error is org.springframework.batch.item.file.transform.IncorrectTokenCountException: Incorrect number of tokens found in record
follow is the code.
How do I continue to read to the third line? I would expect first line read, second line skipped, third line read.
FlatFileItemReader<CustomerBatch> reader = new FlatFileItemReader<>();
DelimitedLineTokenizer delimitedTokeniser = new DelimitedLineTokenizer();
delimitedTokeniser.setNames(new String[]{ "a", "b","c","d","e" });
DefaultLineMapper<CustomerBatch> lineMapper = new DefaultLineMapper<CustomerBatch>();
lineMapper.setLineTokenizer(delimitedTokeniser);
lineMapper.setFieldSetMapper(new BeanWrapperFieldSetMapper<CustomerBatch>() {{setTargetType(CustomerBatch.class);}});
ByteArrayResource y= new ByteArrayResource(batchFile.getBytes());
reader.setResource(y);
reader.setLineMapper(lineMapper);
reader.setLinesToSkip(1); //do not read the first line of the csv file.
reader.open(new ExecutionContext());
try {
while ((newCustomerBatch = reader.read()) != null) {
System.out.println("name:"+newCustomerBatch.getName());
//some logic.
}
}catch(Exception e) {
}
}