Parsing a Fixed-Width Formatted File in Java

Parsing a fixed-width formatted file in Java

I would use a flat file parser like flatworm instead of reinventing the wheel: it has a clean API, is simple to use, has decent error handling and a simple file format descriptor. Another option is jFFP but I prefer the first one.

Java fixed-width file format read/write library

I would use ByteBuffer, possibly with memory mapped files. This allows to read/write primitive type in big or little endian. This option is best for fixed width binary data.

For fixed width text you can use BufferedReader.readLine() and String.substring(from, to) to get the fields you want. To output fixed width fields you can use PrintWriter.printf(format, fields ...).

Parsing multi-line fixed-width files

Have you looked at JRecordBind?

http://jrecordbind.org/

"JRecordBind supports hierarchical fixed length files: records of some type that are 'sons' of other record types."

Is there an smart way to write a fixed length flat file?

If you are still looking for a framework, check out BeanIO at http://www.beanio.org

Reading fixed width text file with different length

We just dealt with this at work within the last few weeks. The way we went about solving the problem was to create an enum class with corresponding "indexes" to represent the start and end positions of the fields that needed to be extracted. This enum is loaded into a map of FIELD_NAME --> RANGE (i.e. 0:8) upon instantiation of the class that parses the message.

High-level, upon receipt of a message on the queue:

  • convert TextMessage to string
  • read line
  • for each field, get the corresponding range from the map
  • split the range on ":" to get the indexes
  • extract the values from the String using substring(index1,index2)
  • perform transformations (string to date, string to numbers, etc)
  • persist to database

Parsing a fixed format flat file using Spring or Java 8

You can do it by org.beanio.spring.BeanIOFlatFileItemReader

batch-context.xml

<bean id="fixedWidthFileReader" class="org.beanio.spring.BeanIOFlatFileItemReader" scope="step">
<property name="streamMapping" value="classpath:META-INF/mapping.xml"/>
<property name="streamName" value="flatFileStream" />
<property name="resource" value="resource/inputFile"/></bean>

mapping.xml

<stream name="flatFileStream" format="fixedlength">
<record name="flatFileContents" class="com.test.YourPojo" order="1">
<field name="header" length="2" />
<field name="name" length="15" />
<field name="supplierCode" length="10" />
</record>

com.test.YourPojo is the domain model.

Note : length in mapping.xml will vary according to your requirement.



Related Topics



Leave a reply



Submit