You can inject a DelimitedLineTokenizer and you can set the delimiter as per requirement. you can make it generic using StepExecutionListener and need to override the beforeStep() method. You will set delimiter in StepExecution.
When you parse the file which are "," seperated then
stepExecution.getExecutionContext().putString("delimiter", ",");
and when file is seperated with "|" then stepExecution.getExecutionContext().putString("delimiter", "|");
But you need to create two jobs. Need to specify listener accordingly.
You can see the example of above explain logic from Spring Batch on Walking Techie
Code for generic Reader:
@Bean
@StepScope
public FlatFileItemReader<Domain> reader(@Value("#{stepExecutionContext[delimiter]}") String delimiter) {
FlatFileItemReader<Domain> reader = new FlatFileItemReader<>();
reader.setResource(new ClassPathResource("sample-data.csv"));
reader.setLineMapper(new DefaultLineMapper<Domain>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[]{"id", "name"});
setDelimiter(delimiter);
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Domain>() {{
setTargetType(Domain.class);
}});
}});
return reader;
}
You can find the many examples on spring batch in spring boot from Spring Batch Tutorial. You will find here all kind of problems related to spring batch.