title: Parsing a fixed format flat file using Spring or Java 8
body: <p>So i have this flat file which i need to parse to get the various fields located within it. For example Position 0 to 2 is a header literal, position 3 to 10 is Name and so on.
  I'm having a hard time trying to figure out how to go about this. Is there anything in Spring Batch that i can use to parse this fixed length file?
  Sample flat file is given below: </p>
  
  <pre><code>H10000001140658760000       16778816471A          422              
  I10000001140658760IT2016060310850028010700030719564-010               L00000072 
  </code></pre>
  
  <p>So in the above format,<br>
  <code>H1</code> is the row literal, <code>0000001140658760</code> is the receipt number,<br>
  <code>LMS</code> is the supplier code and so on.<br>
  I need to map these to various fields like  </p>
  
  <ul>
  <li>position 0 to position 2 is the Literal,   </li>
  <li>position 3 to position 18 is receipt number,   </li>
  <li>position 18 to position 28 is supplier code.</li>
  </ul>
  
  <p>Please advise.</p>
  
  <p>So i tried using BeanIO with Spring Batch Integration but i'm getting an error while trying to create the reader.<br>
  I'm getting     org.springframework.batch.item.ItemStreamException: Failed to initialize the reader.
  Below is my code:</p>
  
  <pre><code>    public BeanIOFlatFileItemReader&lt;String&gt; createBeanIOFlatFileItemReader(File file) throws Exception {
      BeanIOFlatFileItemReader&lt;String&gt; reader = new BeanIOFlatFileItemReader&lt;&gt;();
  
      reader.setStreamFactory(StreamFactory.newInstance());
      reader.setResource(new FileSystemResource(file));
      reader.setStreamMapping(new ClassPathResource("mapping.xml"));
      reader.setStreamName("file");//this name shud be same as the name of the stream in the mapping.xml
      reader.afterPropertiesSet();// Adding this solved the issue!
      reader.open(new ExecutionContext());
      return reader;
  }
  </code></pre>
  
  <p>Below is the mapping file i'm using:</p>
  
  <pre><code>&lt;beanio xmlns="http://www.beanio.org/2012/03" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd"&gt;
  
   &lt;stream name="file" format="fixedlength" minOccurs="0" maxOccurs="unbounded"&gt;
  &lt;record name="employee" class="Employee"  order="1"&gt;
  &lt;/record&gt;
  &lt;record name="company"  class="Company"  order="2"&gt;
  &lt;/record&gt;
   &lt;/stream&gt;
  &lt;/beanio&gt;
  </code></pre>
  
  <p>Here's the Stack Trace:</p>
  
  <pre><code>org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
  at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147)
  at com.gateway.fileloader.fileprocessor.ArchiveFileProcessor.createBeanIOFlatFileItemReader(ArchiveFileProcessor.java:211)
  at com.gateway.fileloader.fileprocessor.ArchiveFileProcessor.doProcess(ArchiveFileProcessor.java:84)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:497)
  at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:113)
  at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:129)
  at org.springframework.expression.spel.ast.MethodReference.access$000(MethodReference.java:49)
  at org.springframework.expression.spel.ast.MethodReference$MethodValueRef.getValue(MethodReference.java:347)
  at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
  at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:131)
  at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:330)
  at org.springframework.integration.util.AbstractExpressionEvaluator.evaluateExpression(AbstractExpressionEvaluator.java:166)
  at org.springframework.integration.util.MessagingMethodInvokerHelper.processInternal(MessagingMethodInvokerHelper.java:317)
  at org.springframework.integration.util.MessagingMethodInvokerHelper.process(MessagingMethodInvokerHelper.java:155)
  at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:93)
  at org.springframework.integration.handler.ServiceActivatingHandler.handleRequestMessage(ServiceActivatingHandler.java:89)
  at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:99)
  at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
  at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
  at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:147)
  at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:120)
  at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
  at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:442)
  at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:392)
  at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
  at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
  at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
  at org.springframework.integration.endpoint.SourcePollingChannelAdapter.handleMessage(SourcePollingChannelAdapter.java:161)
  at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:251)
  at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:57)
  at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:176)
  at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:173)
  at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:330)
  at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:55)
  at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
  at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:51)
  at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:324)
  at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
  at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
  
  Caused by: java.lang.IllegalArgumentException: No stream mapping configured for name 'fileDummy'
  at org.beanio.internal.DefaultStreamFactory.getStream(DefaultStreamFactory.java:129)
  at org.beanio.internal.DefaultStreamFactory.createReader(DefaultStreamFactory.java:70)
  at org.beanio.spring.BeanIOFlatFileItemReader.doOpen(BeanIOFlatFileItemReader.java:129)
  at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144)
  ... 48 more
  </code></pre>