Results 1 to 4 of 4

Thread: Writing many lines from one item in FlatFileItemReader

  1. #1

    Default 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.
    --
    Gaëtan PITTELOUD

  2. #2
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    Couldn't you just call write multiple times?

  3. #3

    Default

    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 ?
    --
    Gaëtan PITTELOUD

  4. #4
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •