0
votes

I have a spring batch job to read a CSV file the file have a header and trailer. To skip the header just skip the first line , but how to skip the footer record ?

this is my reader to simplify :

<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"
        scope="step">

        <property name="resource" value="file:#{jobParameters[file]}" />
        <property name="linesToSkip" value="1" />
        <property name="lineMapper">

            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">

                <property name="fieldSetMapper">

                    <bean class="ben.Mapper" />
                </property>

                <property name="lineTokenizer">

                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="delimiter" value="," />
                    </bean>
                </property>

            </bean>

        </property>

    </bean>
1
B - Refer this: @Duplicate - stackoverflow.com/questions/20956768/…Pra_A

1 Answers

0
votes

Try this:

public class SkipFooterLineMapper extends DefaultLineMapper<XXX> {

    @Override
    public XXX mapLine(String line, int lineNumber) throws Exception {
        // skip the footer as footer like this: T~2952
        if(line != null && line.split("~").length == 2) {  
            return null;
        }
        return super.mapLine(line, lineNumber);
    }

}

FlatFileItemReader<XXX> bean = new FlatFileItemReader<>();
bean.setResource(new FileSystemResource(pathToFile));

// skip the header
bean.setLinesToSkip(1);
bean.setLineMapper(lineMapper());