Announcement Announcement Module
No announcement yet.
A flat file ItemReader/ItemWriter with BeanIO Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • A flat file ItemReader/ItemWriter with BeanIO

    Hi everyone,

    If anyone is interested, I've added Spring Batch (2.1.x) ItemReader/Writer adapters for flat files to the BeanIO 1.2.x open source project. BeanIO supports reading and writing CSV, delimited, fixed length and XML file formats using an XML mapping file to bind a stream layout to Java objects. For more information, you can check out the reference guide at And here is a quick example:

    Given a CSV file such as the following (in.csv):

    Detail,"Joe","Smith",25,100 Main St,,Chicago,IL,12345
    Detail,"Jason",Jones,32,5600 State St,Apt #2,Santa Fe,NM,12345
    And Java bean objects such as:

    public Contact {
        String firstName;
        String lastName;
        int age;
        Address address;
        // getters & setters...
    public Address {
        String street1;
        String street2;
        String city;
        String state;
        String zip;
        // getters & setters...
    A BeanIO mapping file (mapping.xml), such as the one below, can read bean objects (Map and Contact) from the file.

    <beanio xmlns="">
      <!-- Mapping files can declare multiple stream layouts -->
      <stream name="contactFile" format="csv">
        <!-- Binds "Header" records to Map objects, field names are keys -->
        <record name="header" class="map" maxOccurs="1">
          <!-- "rid" stands for record identifier and is used to match record types -->
          <field name="recordType" rid="true" literal="Header" />
          <field name="fileDate" type="date" format="yyyy-MM-dd" />
        <!-- Binds "Detail" records to Contact objects -->
        <record name="contact" class="example.Contact" minOccurs="0">
          <field name="recordType" rid="true" literal="Detail" ignore="true" />
          <field name="firstName" />
          <field name="lastName" />
          <field name="age" />
          <bean name="address" class="example.Address">
            <field name="street1" />
            <field name="street2" />
            <field name="city" />
            <!-- Simple textual validations are supported: -->
            <field name="state" required="true" minLength="2" maxLength="2" />
            <field name="zip" regex="\d{5}" />
    Using the following Spring Batch ItemReader bean configuration:

    <bean id="itemReader" class="org.beanio.spring.BeanIOFlatFileItemReader">
      <property name="streamMapping" value="classpath:/mapping.xml" />
      <property name="streamName" value="contactFile" />
      <property name="resource" value="file:in.csv" />
    And likewise, an ItemWriter can be configured for writing the same objects to an output file:

    <bean id="itemWriter" class="org.beanio.spring.BeanIOFlatFileItemWriter">
      <property name="streamMapping" value="classpath:/mapping.xml" />
      <property name="streamName" value="contactFile" />
      <property name="resource" value="file:out.csv" />
    Of course, much more is possible, although with Spring Batch so extensible, I will refrain from any direct feature comparison because I would no doubt be quickly proven wrong