Sep 12th, 2008, 02:17 AM
Writing many lines from one item in FlatFileItemWriter
I am using the FlatFileItemWriter to write data read by a reader. Each read item results in writing N lines in the file.
What is the strategy to be used in this case ?
My first idea is to set up a ItemTransformer that transforms the item into a List of N sub-items. The list is passed as a single item to the FlatFileItemWriter.write() method; a FieldSetCreator creates a huge FieldSet containing N*nb_fields_per_sub_item fields, and a LineAggregator creates a String containing N lines (i.e. the returned String contains \n characters), with a double nested loop: (for i=0..N) and for (j=0..nb_fields_per_sub_item).
There is however a problem with this : the FlatFileItemReader increments its the linesWritten counter by one for each written line, even if the line contains \n characters.
I really need to the know the exact number of written lines, as each line of the output file must start with the line number.
Last edited by gpitteloud; Sep 12th, 2008 at 07:07 AM.
Sep 12th, 2008, 10:22 AM
Couldn't you just call write multiple times?
Sep 12th, 2008, 10:30 AM
In order to call write() multiple times, I would need to write my own ItemWriter that delegates to the FlatFileItemWriter and invokes the FFIW write() method multiple times in my write() method. While this sounds possible, I was wondering if a solution exists, that allows me to use the FFIW as is (without the need to write my own ItemWriter wrapping it) and just configure it with a special FieldSetCreator and LineAggregator that would do the work.
So, do you think writing my own ItemWriter wrapping a FFIW is a better way to go than using the FFIW as is and configure it with home-made FieldSetCreator and LineAggregator components ?
Sep 12th, 2008, 11:05 AM
I would wrap the FFIW, the other approach is much more complex I think, and I don't see any problem with the composite writer.