The FlatFileService supports flat file creation and consumption. Different row types are supported of which each one can have its own format definition.
A "Flat File" is also known as "fixed-width" file, containing multiple columns of data where each value is filled with spaces until the fixed width for this column is reached.
A flat file service instance can be configured using the standard AludraTest Configuration Mechanism. The properties base name is flatfile
.
The only configuration element provides a locale definition which must conform the Java locale naming conventions, e.g. en_US or de_DE
Example:
locale=en_US
The locale is used for basic setups of date and number formatting.
If no such configuration property is set, the framework assumes en_US by default.
The FlatFileService supports the use of so-called 'FlatFile Beans' which carry plain data content as well as a column format specification for their associated flat file type.
FlatFile Beans must comply with the following conditions:
FlatFileColumn
annotationA FlatFileBean's attributes are configured to be formatted/parsed in a specific format by attaching a @FlatFileColumn annotation.
Example:
@FlatFileColumn(startIndex = 21, format="20") public String city;
The startIndex denotes the character offset from the beginning of the row. The first column has the index 1.
The format defines the pattern for formatting/parsing the attribute. It may be specified in three different manners:
DyyyyMMdd
N000.00
Examples are:
All attributes of a FlatFileBean must be configured to be formatted without gaps and overlaps in the @FlatFileColumn formats. The lowest startIndex must be 1.
An example with a simple Address class:
public class FFAddress { @FlatFileColumn(startIndex = 1, format="20") public String street; @FlatFileColumn(startIndex = 21, format="20") public String city; @FlatFileColumn(startIndex = 41, format="N0000.00") public double num; public FFAddress() { this(null, null, 0); } public FFAddress(String street, String city, double num) { this.street = street; this.city = city; this.num = num; } }
Flat-File-Beans like the FFAddress above can be written using an appropriate FlatFileWriter, e.g. an FFAddressWriter:
FlatFileService service = getService(ComponentId.create(FlatFileService.class, "fftest")); FFAddressWriter writer = new FFAddressWriter(service); writer.writeAddress(new FFAddress("MAIN STREET 321", "NEW YORK", 123.45)); writer.writeAddress(new FFAddress("2ND STREET 4321", "JERSEY", 3.8)); writer.close();
Flat files can only be read using an related reader, e.g. an FFAddressReader:
FlatFileService service = getService(ComponentId.create(FlatFileService.class, "fftest")); FFAddressReader reader = new FFAddressReader(service); FFAddress address; while ((address = reader.readRow() = null) { System.out.println(address); } reader.close();