Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
248 lines (188 sloc) 6.8 KB

What is JFileHelpers?

Chances are every programmer will end up having to manipulate structured text files at any point in his career. Those files are called flat files and, until now, you had to go through that same boring script to work with them. You had to open the file, create a reader, then start reading and parsing line by line. No more.

JFileHelpers is a library that automates the tedious task of parsing and creating structured text files. It handles fixed width or delimited files with Java annotations sweetness.

JFileHelpers started as a port of the awesome Marcos Meli's FileHelpers but it's starting to take its own path and we are in need of passinate developers who would like to give us a hand.

Go ahead and try it out!

The latest binary release can always be found on the Releases area.

So what's code like?

Let's take, for instance, a fixed length structured text file, that handles customer data (you can see the ever-growing list of examples as well).

Here's how our bean should be:

public class Customer {
    public Integer custId;

    public String name;

    public Integer rating;

    @FieldConverter(converter = ConverterKind.Date,
        format = "dd-MM-yyyy")
    public Date addedDate;

    public String stockSimbol;

This could would handle a text file structured like this:

1   Antonio Pereira     10012-12-1978ABC
2   Felipe Coury          201-01-2007
3   Anderson Polga       4212-11-2007DEF

And reading that file is as easy as:

  FileHelperEngine<Customer> engine =
      new FileHelperEngine<Customer>(Customer.class);
  List<Customer> customers =
      new ArrayList<Customer>();

  customers = engine.readResource(

This way, you can manipulate any properties of the beans contained on the ArrayList collection and eventually, even recreate a file with same format, also as easy as:

FileHelperEngine<Customer> engine =
    new FileHelperEngine<Customer>(Customer.class);
List<Customer> customers = new ArrayList<Customer>();

customers = engine.readResource(

// retrieves customer 3 - Anderson Polga
Customer c = customers.get(2);
// changes a couple of properties
c.rating = 82;
c.stockSimbol = "APR";

// and removes first customer - Antonio Pereira

// writes the output file
engine.writeFile("customers-fixed-out.txt", customers);

As you may have already anticipated, the output file will look like this:

   2        Felipe Coury  201-01-2007
   3      Anderson Polga 8212-11-2007APR


Enum conversion

This library has the out-of-the-box ability to export and import values to/from Java enum classes.

Take the following enum as an example:

public enum Enum2 { One, Two, Three }

If we have an input file containing the following lines:


And a very simple bean like this:

public class EnumType2 {
    public Enum2 enumValue;

It would be natural to write code for loading the file:

public static void main(String[] args) throws IOException {
    FileHelperEngine engine =
        new FileHelperEngine<EnumType2>(EnumType2.class);

    List<EnumType2> res =

    System.out.println("Size: " + res.size());


... and that would print out:

Size: 5


See how easy it is to handle master-detail formatted files:

ALFKI|Alfreds Futterkiste|Maria Anders|Sales Representative|Obere Str. 57|Berlin|Germany
ANATR|Ana Trujillo Emparedados y helados|Ana Trujillo|Owner|Avda. de la Constitución 2222|México D.F.|Mexico
ANTON|Antonio Moreno Taquería|Antonio Moreno|Owner|Mataderos  2312|México D.F.|Mexico
DUMON|Du monde entier|Janine Labrune|Owner|67, rue des Cinquante Otages|Nantes|France

To indicate what records are to be considered as master as what are to be considered as detail, we use the following code:

engine = new MasterDetailEngine<CustomersVerticalBar,
    OrdersVerticalBar>(CustomersVerticalBar.class, OrdersVerticalBar.class,

        new MasterDetailSelector() {

            public RecordAction getRecordAction(String recordString) {
                // if the first char on the record is a letter
                // we'll consider it a master record and
                // if not, we'll consider it a detail record
                if (Character.isLetter(recordString.charAt(0)))
                    return RecordAction.Master;
                    return RecordAction.Detail;


List<MasterDetails<CustomersVerticalBar, OrdersVerticalBar>> res =
    (List<MasterDetails<CustomersVerticalBar, OrdersVerticalBar>>)
        Common.readTest(engine, "Good/MasterDetail1.txt");

And here's how the beans are:

public class CustomersVerticalBar
    implements ComparableRecord<CustomersVerticalBar> {

    public String customerID;
    public String companyName;
    public String contactName;
    public String contactTitle;
    public String address;
    public String city;
    public String country;

    public boolean equalsRecord(CustomersVerticalBar record) {
        if (this.customerID == null) {
            return false;
        return this.customerID.equals(record);


public class OrdersVerticalBar {

    public int orderID;
    public String customerID;
    public int employeeID;
    public Date orderDate;
    public Date requiredDate;
    public Date shippedDate;
    public int shipVia;
    public float freight;