Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Parse a delimited flat file to a POJO [closed]

Tags:

java

Appreciate if someone could point and recommend on how to parse a flat pipe delimited file to JAVA Pojo.

eg. Flat File 0001|XYZ|120

this is required to be read into a POJO having

public class pojo {

private String acct;
private String customer;
private int balance;
}

I can read entire input file as a collection, however, would end up setting each token into a pojo member. Instead, I would like to parse into pojo members. Something similar to CASTOR XML mapping to POJO.

Appreciate any help in this regards. Thanks in advance

like image 876
Anand Avatar asked Dec 15 '22 16:12

Anand


2 Answers

You can use Bean IO. I have been using this extensively.

Configure your XML as something like this

<stream name="employees" format="delimited" strict="true">
  <parser>  
    <property name="delimiter" value="|" />
  </parser>
  <record name="test" class="example.Pojo" minOccurs="1" maxOccurs="1">
      <field name="act" />
      <field name="customer" />
      <field name="balance" type="int" />
    </record>
 </stream>

Refer here for more details.

like image 84
Jayamohan Avatar answered Dec 18 '22 05:12

Jayamohan


OopenCSV http://opencsv.sourceforge.net/ has what you are looking for. Just change the delimiter to | from ,. And you should be all set.

like image 45
Thihara Avatar answered Dec 18 '22 04:12

Thihara