title: Spring Batch- Read Footer in fixed length file
body: <p>I wanted to validate the number of records in the file be equal to the number mentioned in the footer row.</p>
  
  <p>is there any inbuilt functionality within spring batch to read fixed-width file footer row?</p>
  
  <p>I tried to use the custom fieldset mapper to validate the number of records to be same as mentioned in the footer row.</p>
  
  <pre><code>new FlatFileItemReaderBuilder&lt;FieldSet&gt;()
                  .name("personItemReader")
                  .resource(fileName)
                  .fixedLength()
                  .columns(getRange())
                  .names(getNames())
                  .fieldSetMapper(new PassThroughFieldSetMapper())
                  .linesToSkip(1)
                  .build();
  
  public class PersonFileReader implements ItemStreamReader&lt;Person&gt; {
  
  private final ItemStreamReader&lt;FieldSet&gt; itemStreamReader;
  private int recordCount = 0;
  private int expectedRecordCount = 0;
  private StepExecution stepExecution;
  private final Logger logger = LoggerFactory.getLogger(PersonFileReader.class);
  
  
  
   public PersonFileReader(@Qualifier("flatFileReader") ItemStreamReader itemStreamReader) {
          this.itemStreamReader = itemStreamReader;
      }
  
      @Override
      public Person read() throws Exception {
          return process(itemStreamReader.read());
      }
  
    //code removed
  
      @AfterStep
      public ExitStatus afterStep(StepExecution execution) {
          if (recordCount == expectedRecordCount) {
              return execution.getExitStatus();
          } else {
              return ExitStatus.STOPPED;
          }
      }
  
  
      private Person process(FieldSet fieldSet) {
          Person result = null;
          if (fieldSet != null) {
              if (fieldSet.getFieldCount() &gt; 1) {
  
                  logger.info("fieldSet", fieldSet.getFieldCount());
  
                  result = Person.builder()
                          .name(fieldSet.readString(0))
                          .lastName(fieldSet.readString(1))
                          .age(fieldSet.readInt(2))
                          .salary(fieldSet.readDouble(3))
                          .houseNumber(fieldSet.readInt(4))
                          .line1(fieldSet.readString(5))
                          .line2(fieldSet.readString(6)).build();
  
  
                  recordCount++;
              } else {
                  expectedRecordCount = fieldSet.readInt(0);
  
                  if (expectedRecordCount != this.recordCount) {
                      this.stepExecution.setTerminateOnly();
                  }
              }
          }
  
          return result;
      }
  }
  </code></pre>
  
  <p>But this fails to read the footer with an exception.</p>
  
  <pre><code>ogg.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 6 in resource=[file [D:\Data\Study\spring-batch\batch-process\src\main\resources\static\file1.txt]], input=[4]
  Caused by: org.springframework.batch.item.file.transform.IncorrectLineLengthException: Line is shorter than max range 66
  </code></pre>
  
  <p><strong>Sample File.</strong></p>
  
  <pre><code>Name        lastName         age salary HouseNo AddLine1  AddLine2
  Andrew         Green          25 26000     10 Avenue      Glasgow 
  Ben            Brown          32 28500     11 Avenue      Glasgow 
  Celia          Black          29 28000     12 Avenue      Glasgow 
  Steve          Pink           41 52000     13 Avenue      Glasgow 
  4
  </code></pre>