BeanIO tutorial
BeanIO is an open source Java framework for marshaling and marshaling Java beans from a flat file, stream, or simple String object. It is very powerful with support for XML, CSV, delimited and fixed length stream formats, Object binding, filed validation, integration with spring-batch, etc. Here is a basic tutorial to get started.
Step 1: The sample CSV file to convert to an object of type Person. The first record is a header record, and the subsequent ones are detail records. The file is person.csv under src/main/resources/data
H, 2013-03-12 John,Smith, FAMILY Peter,Smith, FAMILY Gregory,Smith, FAMILY
Step 2: Define the dependency jar in pom.xml file.
<!-- beanio --> <dependency> <groupId>org.beanio</groupId> <artifactId>beanio</artifactId> <version>2.0.2</version> </dependency>
Step 3:The main class that wires up everything.
package com.mycompany.app10;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.beanio.BeanReader;
import org.beanio.StreamFactory;
public class BeanIoMain
{
public void readCSVFileUsingBeanIo()
{
StreamFactory factory = StreamFactory.newInstance();
factory.loadResource("person.xml");
//read it from the classpath : src/main/resources
InputStream in = this.getClass().getResourceAsStream("/data/persons.csv");
BeanReader reader = factory.createReader("persons", new InputStreamReader(in));
Object record = null;
List<Person> persons = new ArrayList<Person>();
// read records from "input.csv"
while ((record = reader.read()) != null)
{
if ("header".equals(reader.getRecordName()))
{
@SuppressWarnings("unchecked")
Map<String, Object> header = (Map<String, Object>) record;
System.out.println(header.get("fileDate"));
}
else if ("detail".equals(reader.getRecordName()))
{
Person person = (Person) record;
persons.add(person);
}
}
System.out.println(persons);
}
public static void main(String[] args)
{
new BeanIoMain().readCSVFileUsingBeanIo();
}
}
Step 3: The POJO Person class.
package com.mycompany.app10;
public class Person
{
private String firstName;
private String surname;
private PersonType type;
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getSurname()
{
return surname;
}
public void setSurname(String surname)
{
this.surname = surname;
}
public PersonType getType()
{
return type;
}
public void setType(PersonType type)
{
this.type = type;
}
@Override
public String toString()
{
return "Person [firstName=" + firstName + ", surname=" + surname + ", type=" + type + "]";
}
}
Step 4: The person.xml under src/main/resources where you can map to your POJO Person.java.
<beanio xmlns="http://www.beanio.org/2012/03"> <stream name="persons" format="csv"> <parser> <property name="delimiter" value="," /> <property name="lineSeparator" value="\n" /> <property name="whitespaceAllowed" value="true" /> </parser> <typeHandler name="personTypeHandler" class="com.mycompany.app10.PersonTypeHandler"/> <record name="header" class="map" maxOccurs="1"> <!-- 'rid' indicates this field is used to identify the record --> <field name="recordType" rid="true" literal="H" /> <!-- 'format' can be used to provide Date and Number formats --> <field name="fileDate" type="date" format="yyyy-MM-dd" /> </record> <record name="detail" minOccurs="0" maxOccurs="unbounded" class="com.mycompany.app10.Person"> <field name="firstName" /> <field name="surname" /> <field name="type" typeHandler="personTypeHandler" type="com.mycompany.app10.PersonType" /> </record> </stream> </beanio>
Step 5: Next, the handler class that parses Enums to String.
package com.mycompany.app10;
import org.beanio.types.TypeConversionException;
import org.beanio.types.TypeHandler;
public class PersonTypeHandler implements TypeHandler
{
public Object parse(String text) throws TypeConversionException
{
PersonType personType = PersonType.valueOf(text.trim());
return personType;
}
public String format(Object value)
{
return value != null ? ((PersonType) value).name() : PersonType.SINGLE.name();
}
public Class<?> getType()
{
return PersonType.class;
}
}
Step 6: The output of the BeanIoMain is:
Tue Mar 12 00:00:00 EST 2013 [Person [firstName=John, surname=Smith, type=FAMILY], Person [firstName=Peter, surname=Smith, type=FAMILY], Person [firstName=Gregory, surname=Smith, type=FAMILY]]
Labels: Java I/O

2 Comments:
The piece of code was missing. PersonType is not listed in this code list.
You are right. It will be an enum class like MALE, FEMALE
Post a Comment
Subscribe to Post Comments [Atom]
<< Home