Search code examples
javaspringspring-batchfile-read

Read file content which start with # using Spring Batch


I have a very simple Spring Batch application which reads multiple file and write it to one file. My project is working super fine in all scenario except if a line start with "#" in my file. My reader doesn't read that line. Problem is that the upper system is going to send down the files where everyline start with # :(

Does anyone faced similar issue and how to solve it.

Thanks in advance..

My tokenizerconfig

<bean id="accountDataTokenizer" class="org.springframework.batch.item.file.transform.PatternMatchingCompositeLineTokenizer">
    <property name="tokenizers">
        <map>
            <entry key="#ACCOUNT*" value-ref="headerRecordTokenizer" />
            <entry key="*" value-ref="defaultLineTokenizer" />
        </map>
    </property>
</bean>


Solution

  • The FlatFileItemReader provides the ability to set a string that identifies commented out lines. This is done via the FlatFileItemReader#setComments(String[] prefixes) configuration. So in your case, you'd configure your FlatFileItemReader as follows:

    @Bean
    public FlatFileItemReader reader() {
        FlatFileItemReader reader = new FlatFileItemReader();
        ...
        reader.setComments(new String[] {"#"});
        return reader;
    }
    

    You can read more about the FlatFileItemReader and this method in the documentation here: https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/file/FlatFileItemReader.html