Announcement Announcement Module
Collapse
No announcement yet.
FlatFileItemReader with optional data Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • FlatFileItemReader with optional data

    I've implemented a FlatFileItemReader that needs to read a file which contains optional data. For example, my data file contains firstName, middleInitial and lastName. A value for middleInitial is not always present. When I call fieldset.readChar("middleInitial") and a value is , I get this error: java.lang.IllegalArgumentException: Cannot convert field value '' to char.

    What's the best way to handle 'optional data' in a fixed length file?

  • #2
    Couldn't you override the default FixedLengthTokenizer.doTokenize(String line).

    Also make sure to setStrict to false on your FixedLengthTokenizer.
    FixedLengthTokenizer.setStrict(false)

    Jeff

    Comment


    • #3
      Originally posted by visualjeff View Post
      Couldn't you override the default FixedLengthTokenizer.doTokenize(String line).

      Also make sure to setStrict to false on your FixedLengthTokenizer.
      FixedLengthTokenizer.setStrict(false)

      Jeff
      Overriding doTokenize(String line) won't help me. The returned String[] from this method contains the proper values with my optional data element being an empty String. (as is should - no value exists in the file). The problem for me, lies in the FieldSet interface and the method readChar(int idx).

      Here's the implementation I'm using from org.springframework.batch.item.file.transform.Defa ultFieldSet

      public char readChar(int index) {
      String value = readAndTrim(index);
      Assert.isTrue(value.length() == 1, "Cannot convert field value '" + value + "' to char.");
      return value.charAt(0);
      }

      I guess I need to override this method but can't figure out how to inject it in my FieldSetMapper implementation. Thoughts???

      Comment


      • #4
        The solution I came up with was:
        Change my domain object instance variable from Character to String
        and called fieldSet.readRawString(int idx) in my FieldSetMapper implementation.

        readRawString doesn't trim the String value.

        Comment


        • #5
          Very good. That solution should do the trick.

          Jeff

          Comment


          • #6
            Originally posted by jpmango View Post
            I guess I need to override this method but can't figure out how to inject it in my FieldSetMapper implementation. Thoughts???
            It is also possible to implement custom FieldSetFactory, which will return extended DefaultFieldSet.

            Code:
            	<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
            		<property name="lineMapper">
            			<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            				<property name="lineTokenizer">
            					<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
            						<property name="fieldSetFactory">
            							<bean class="org.springframework.batch.item.file.transform.DefaultFieldSetFactory" />
            						</property>
            						...
            					</bean>
            				</property>
            				...
            			</bean>
            		</property>
            		...
            	</bean>

            Comment

            Working...
            X