1

I am reading data from fixed length file.

This is file content:
Joe Smith Developer 075000 10012009

This is the mapping file:

    <beanio>

      <stream name="employeeFile" format="fixedlength">
        <record name="employee" class="Employee" minOccurs="0" maxOccurs="unbounded">
          <field name="firstName" length="10" />
          <field name="lastName" length="10" />
          <field name="title" length="10" />
          <field name="salary"  length="6" />
          <field name="hireDate" format="MMddyy"  minOccurs="0" length="unbounded" maxLength="8"/>
        </record> 
      </stream>

    </beanio>

output:

    First Name:Joe
    Last Name:Smith    
    Title:Developer 
    Salary:75000
    Hire Date:Thu Oct 01 00:00:00 IST 2009

The code is reading the file and converting to pojo successfully. Now client needs strange requirement where I am struggling to implement.

From the file content "Joe Smith Developer 075000 10012009" if the last values are not appearing or appearing partially still code should read the content successfully.

Eg: If the file has content like "Joe Smith Developer 0750". Here salary length is 4 but we declared as 6 in the mapping file and there is no hiredate data. Still, the code should read it successfully like it should take a salary as 0750 and hiredate as null.

How can I read this?

recnac
  • 3,744
  • 6
  • 24
  • 46
s.k Syed
  • 31
  • 3
  • The format of the date you specify in the `mapping.xml` does not match your input, please check and fix it. – nicoschl Jun 13 '19 at 11:02
  • Yeah I am aware of it I am using the correct format like format="MMddyyyy". That is not bothering me. My question is how can I read data which has lesser length than the length mentioned in mapping file? eg: if this is the file content "Joe Smith Developer 0750" and declared mapping file as – s.k Syed Jun 13 '19 at 14:41
  • and declared mapping file as – s.k Syed Jun 13 '19 at 14:51
  • in Mapping file salary length is mentioned as 6. but in my file salary length is of 4 and there is no hire date. Still I need to read the Salary as 75 and hireData as null. How can I achieve this? – s.k Syed Jun 13 '19 at 14:52

1 Answers1

1

The best I can get is to have it assign null values to fields that are missing. The moment any part of a field is present, the whole length of that field must be present in the data. This is the nature of fixed length formats.

Using this mapping file:

<beanio>
  <stream name="employeeFile" format="fixedlength">
    <record name="employee" class="Employee" minOccurs="0" maxOccurs="unbounded">
      <field name="firstName" length="10"/>
      <field name="lastName" length="10"/>
      <field name="title" length="10"/>
      <field name="salary" length="6" minOccurs="0"/>
      <field name="hireDate" format="MMddyyyy" length="8" minOccurs="0"/>
    </record>
  </stream>
</beanio>

You will be able to read data such as:

Joe1      Smith     Developer 07500010012009
Joe3      Smith     Developer 

Outputs:

Employee(firstName=Joe1, lastName=Smith, title=Developer, salary=075000, hireDate=Thu Oct 01 00:00:00 SAST 2009)
Employee(firstName=Joe3, lastName=Smith, title=Developer, salary=null, hireDate=null)

But this line will not work (as you know and hence the reason for this question)

Joe2      Smith     Developer 0750

You either have to tell the people/company that supply the data to conform to the data specification or you will have to manipulate the data before you can use BeanIO to read the data. Otherwise the data must be supplied in some variable length format such as CSV, a pipe delimited or XML that you can then parse properly with BeanIO

nicoschl
  • 2,346
  • 1
  • 17
  • 17