'Write the same data with different file names using single Item writer using Spring Batch

I have a Spring Batch FlatFileItemWriter which writes data to a specific location with a file name(let's say A.txt for example). Now I need the same file to be written with different file name (let's say B.txt) . How can I achieve this with a single item writer? Is it possible?If not are there any alternative solutions?The below is my code

 public FlatFileItemWriter<MyRecord> myItemWriter(@Qualifier("myAggregator") LineAggregator<MyRecord> lineAggregator) throws IOException {

    String fileName= this.localTempPath + fileProperties.myLength() + getTime();
    


    return new FlatFileItemWriterBuilder<MyRecord>()
            .name("my_itemWriter")
            .resource(new FileSystemResource(//path of resource)
            .lineAggregator(lineAggregator)
            .encoding(fileProperties.getEncodingMyLength())
            .append(true)
            .lineSeparator("\r\n")
            .build();
}


Solution 1:[1]

I have a Spring Batch FlatFileItemWriter which writes data to a specific location with a file name(let's say A.txt for example). Now I need the same file to be written with different file name (let's say B.txt) . How can I achieve this with a single item writer? Is it possible?

You can use a CompositeItemWriter that delegates to two FlatFileItemWriters (one for each file).

If not are there any alternative solutions?

As mentioned above, it is possible, but an alternative could be adding a tasklet step that duplicates the file with a different name.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mahmoud Ben Hassine