Oct 25, 2010 - The Spring Batch Admin Project. Item Error Handling. Writing Your Own ItemProcessor.
By Arvind Rai, November 08, 2014 This page is describing the use of FlatFileItemReader and FlatFileItemWriter using annotation in spring bath 3. We will create the bean for FlatFileItemReader and FlatFileItemWriter. Flat file resources can be read and written using ClassPathResource or FileSystemResource. If using ClassPathResource, we need to put file in classpath and if using FileSystemResource we can set even absolute path. Project Configuration in Eclipse Find the project structure in eclipse. Create Bean for FlatFileItemReader To create FlatFileItemReader we need to do below steps.
Set Resource We need to set flat file resource to read data from the file. To read resource we can use ClassPathResource or FileSystemResource etc. Set LineMapper We can use DefaultLineMapper to set in FlatFileItemReader instance.
DefaultLineMapper has a method setLineTokenizer that needs to be defined. We can use DelimitedLineTokenizer here to set.
Define a method setNames of DelimitedLineTokenizer. To define setNames method, we need to pass field names of input resource. Set FieldSetMapper Define BeanWrapperFieldSetMapper class. This class has a method setTargetType that need to be set. Target type is the class which is being used to read input. Create Bean for FlatFileItemWriter To create FlatFileItemWriter bean, find the below steps which are the setter methods that define the data required by FlatFileItemWriter. Set Resource Set a flat file resource for the output.
We can use ClassPathResource or FileSystemResource etc. To get resource instance. Set LineAggregator In our example we are using DelimitedLineAggregator. Instantiate this class and set values for two methods, setDelimiter and setFieldExtractor. In setDelimiter method we set the token on the basis of which, values will be delimited. FieldExtractor consist the name of fields defined in the class which is representing output data.
Run the Example and check the Output Download the complete source code from the download link on the bottom. Put the two input and output file in classpath as mentioned in configuration class. Use the below class to run the example.