My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Wiki pages
Links

BeanFiles is a java library for importing and exporting javabeans to/from various file formats. Right now, it only supports importing from csv using opencsv and xls using POI. Check out the examples.

Beanfiles aims to facilitate type conversion and multiple input/output formats while reusing some of the underlying translation components.

The library currently supports importing only, but has some useful features. It supports the following:

  • No configuration (if column headers match bean property names)
  • Property Mapping (if column headers do not match bean property names)
  • Nested property resolutions (bean.childBean.property.whatever)
  • Simple wildcard mappings (image* will build a list of strings where column header is imageXyz)
  • Easily add custom translators (for instance a column is an id, and the bean property is a lookup)

A nice use-case is creating beans with the beanfiles library, then using Hibernate to store objects to a relational model.

There's a Google group for discussing this project:

http://groups.google.com/group/beanfiles

Example Usage

Consider the following csv file:

property1,property2,property3,property4,property5
line1_prop1,line1_prop2,line1_prop3,1,FALSE
line2_prop1,line2_prop2,line2_prop3,2,TRUE

In order to map this data to the following Java Bean:

public class SimpleBean {

    private String property1;
    private String property2;
    private String property3;
    private int property4;
    private boolean property5;

    public SimpleBean() {}

    public String getProperty1() {
        return property1;
    }

    public void setProperty1(String property1) {
        this.property1 = property1;
    }

    ...
}

Here's the Beanfiles code:

        InputStream input = getInputStream("files/csv/simple_properties.csv");
        ReaderIterator<SimpleBean> iterator = new CSVReaderIterator<SimpleBean>(SimpleBean.class, input);
        input.close();

        SimpleBean bean1 = iterator.next();
        SimpleBean bean2 = iterator.next();
Powered by Google Project Hosting