Fixedformat4j
Fixedformat4j is an easy to use Java framework for working with flat fixed formatted text files. By annotating your code you can setup the offsets and format as for your data when reading/writing to and from flat fixed format files.
Fixedformat4j handles the following build in datatypes:
- String
- Character / char
- Long / long
- Integer / int
- Double / double
- Float /float
- Boolean / boolean
- Date
- BigDecimal
At the same time it is straight forward to write and plugin your own formatters for custom datatypes.
Getting started
To start using Fixedformat4j you only have to annotate your getter methods and use the FixedFormatManager to load and export your fixedformat text according to your annotation instructions.
A sample can be seen here:
@Record
public class BasicRecord {
private String stringData;
private Integer integerData;
private Date dateData;
@Field(offset = 1, length = 10)
public String getStringData() {
return stringData;
}
public void setStringData(String stringData) {
this.stringData = stringData;
}
@Field(offset = 11, length = 5, align = Align.RIGHT, paddingChar = '0')
public Integer getIntegerData() {
return integerData;
}
public void setIntegerData(Integer integerData) {
this.integerData = integerData;
}
@Field(offset = 16, length = 10)
@FixedFormatPattern("yyyy-MM-dd")
public Date getDateData() {
return dateData;
}
public void setDateData(Date dateData) {
this.dateData = dateData;
}
}
This annotated class can now be loaded and exported using a FixedFormatManager
public class BasicUsage {
private static FixedFormatManager manager = new FixedFormatManagerImpl();
public static void main(String[] args) {
String string = "string 001232008-05-29";
BasicRecord record = manager.load(BasicRecord.class, string);
System.out.println("The parsed string: " + record.getStringData());
System.out.println("The parsed integer: " + record.getIntegerData());
System.out.println("The parsed date: " + record.getDateData());
record.setIntegerData(100);
System.out.println("Exported: " + manager.export(record));
}
}
Running this simple program will generate the following output.
Note that the integer changed value in the exported string.
The parsed string: string The parsed integer: 123 The parsed date: Thu May 29 00:00:00 CEST 2008 Exported: string 001002008-05-29
Authors and Contributors
Fixedformat4j was originally started by Jacob von Eyben (@jeyben). Per Olsen (@polesen) has contributed several times.