Write Annotated Class FixedLength BeanIO

Write Annotated Class FixedLength BeanIO – In this example we will see how to write FixedLenght data into file using Java class with Annotation. Most people use XML file for mapping which is very tedious as you will have to main separate file for it. To keep it to very simple I will be using class with annotation which provide all functionality as XML and we don’t need to maintain separate file:

Note: We will use BeanIO to accomplish this

2. First write sample POLO class as below:

@Record
public class JavahonkPojo {

    @Field(ordinal = 1, length = 6)
    private String firstName;
    @Field(ordinal = 2, length = 6)
    private String lastName;
    @Field(ordinal = 3, length = 9)
    private String role;
    @Field(ordinal = 5, format="yyyy-MM-dd HH:mm:ss.SSS'Z'", length = 26)
    private Date dateOfBirth;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    @Override
    public String toString() {
        return "JavahonkPojo{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", role='" + role + '\'' +
                ", dateOfBirth=" + dateOfBirth +
                '}';
    }
}

2. Now write main class: BeanIOAnnotation.java which will setup factory and will set data into JavahonkPojo class and use BeanWrite to write output:

import org.beanio.BeanWriter;
import org.beanio.StreamFactory;
import org.beanio.builder.FixedLengthParserBuilder;
import org.beanio.builder.StreamBuilder;

import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;

public class BeanIOAnnotation {

    public static void main(String[] args) {
        StreamFactory factory = StreamFactory.newInstance();
        StreamBuilder fixedLengthBuilder = new StreamBuilder("JavaHonk")
                .format("fixedlength")
                .parser(new FixedLengthParserBuilder())
                .addRecord(JavahonkPojo.class);
        factory.define(fixedLengthBuilder);

        //This will write content in file
        //BeanWriter out = factory.createWriter("JavaHonk", new File("Javahonk.txt"));

        //This will write content in String
        Writer writer = new StringWriter();
        BeanWriter out = factory.createWriter("JavaHonk", writer);

        JavahonkPojo javahonkPojo = new JavahonkPojo();
        javahonkPojo.setFirstName("123456789");
        javahonkPojo.setLastName("123456789");
        javahonkPojo.setRole("Architect");
        javahonkPojo.setDateOfBirth(new Date());

        out.write(javahonkPojo);
        System.out.println(writer.toString());
        out.flush();
        out.close();

    }
}

3. Output:

Write Annotated Class FixedLength BeanIO

4. Note: If you are interested to write FixedLength content into file please use below which I have included in the code:

BeanWriter out = factory.createWriter(“JavaHonk”, new File(“Javahonk.txt”));

5. OR: If you are interested to write fixedLength content into String then please use included in the code as well:

//This will write content in String
        Writer writer = new StringWriter();
        BeanWriter out = factory.createWriter("JavaHonk", writer);

        JavahonkPojo javahonkPojo = new JavahonkPojo();
        javahonkPojo.setFirstName("123456789");
        javahonkPojo.setLastName("123456789");
        javahonkPojo.setRole("Architect");
        javahonkPojo.setDateOfBirth(new Date());

        out.write(javahonkPojo);
        System.out.println(writer.toString());

6. Reference: Write Annotated Class FixedLength BeanIO – BeanIO FixedLength

One thought on “Write Annotated Class FixedLength BeanIO”
  1. this post is vary helpful for me. thanks a lot.
    could you please give me example for reader – to read from FixedLength field file

Leave a Reply

Your email address will not be published. Required fields are marked *