Announcement Announcement Module
No announcement yet.
LineAggregator - Fixed length output Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • LineAggregator - Fixed length output

    Hello all,

    Can someone tell me how can I output Fixed length line without a transformer?

    For ex, in order to create a bean by reading a file I can define FixedLengthTokenizer and attach that to my FlatFileItemReader and specify my mapper.

    Is there something similar for writing it out?? Instead me creating a transformer? LineAggregator helps me to specify the location of fields but which field should go where? Where do I specify that in config file?


  • #2
    In reading this, I noticed that the FlatFileItemWriter section in the docs is a bit light. I've created an issue to track the update:

    The FixedLengthLineAggregator works in many ways like the FixedLengthLineTokenizer. You specify the ranges you want to write out and it will write each field in the FieldSet into each range, with the appropriate overall alignment. It's worth noting that there are a few enhancements in the works for 1.1 to allow for setting different alignments for individual fields, but for now, in 1.0.x, they can only be set for the entire line.


    • #3
      Thanks Lucas!

      In the meanwhile, can you let me know how can I use FieldSet in the following case:

      I have my writer defined as:

      <property name="itemWriter">
      <bean class="org.springframework.batch.item.file.FlatFil eItemWriter"
      <property name="resource" ref="outputFileLocator" />
      <property name="lineAggregator" ref="outputLineAggregator" />
      <property name="fieldSetCreator">
      <bean class="org.springframework.batch.item.file.mapping .PassThroughFieldSetMapper" />

      Here is my aggregator:

      <bean class="org.springframework.batch.item.file.transfo rm.FixedLengthLineAggregator" id="outputLineAggregator">
      <property name="columns" value="1-5, 6-11, 12-21"></property>

      Right now I have the passthroughfieldset, but would like to map my fields to the corresponding positions? How do I map a particular field to 1-5?



      • #4
        I'm a bit confused. The only reason to use a PassThroughFieldSetMapper, is if you are giving the Writer a FieldSet directly. If not you can write your own FieldSetCreator, that knows how to convert from your particular object to a FieldSet.

        but would like to map my fields to the corresponding positions? How do I map a particular field to 1-5?
        I don't understand what you mean by this? The ranges correspond to the indexes of the FieldSet. So range 1-5 corresponds to the first field in the fieldSet. (i.e. fieldSet.readString(0) ) You don't really have to do anything but provide the ranges for each index, which it looks like you have done.


        • #5
          Ok, sorry about the confusion!! I got your point that I should be creating my own FieldSetCreator if I need to write my domain object to a file.

          In the above code, actually my intention is to replace the PassThroughFieldSetMapper.

          Here is an example of what I am trying to do:

          Consider I have the following strings in my domain object "xyz", "abcde"
          and "q". This is my first field and in the outputLineAggregator I have specified that first field is 1-5. Will it make sure that for all the above values it writes five characters?? For ex. for "xyz" it should append/prefix two spaces depending upon the justification and for "abcde" it should not append/prefix any spaces and for "q" it should append/prefix four spaces.

          Along with that if my padding is " " (space) even that should be added before the second field.

          Do we have that feature yet? If yes, can you give me some lead on where should I look?



          • #6
            You have to give ranges for all three, so: "1-5,6-10,11-15". You can also declare an alignment for all three. If you declare it to be left aligned, the text will all be on the left. So in your example the following would be written:


            Where '-' equals a space. (The forum had issues with showing the spaces correctly)

            There is an enhancement set for 1.1 to allow for declaring these alignments on a per record basis, which would really only be useful when mixing text and strings, since you would probably want to right align numbers and pad with 0's, and left align text, padding with spaces.
            Last edited by lucasward; Apr 30th, 2008, 09:56 PM. Reason: Fixed spacing