FlatFileService User Guide

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.

Configuration

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.

FlatFile Beans

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 Bean Requirements

FlatFile Beans must comply with the following conditions:

  • Have a public no-arg constructor
  • Provide any data by public properties (Getters / Setters)
  • Annotate each attribute to be exported / loaded with a FlatFileColumn annotation
  • Have a 'flat' structure; no navigation among bean property graphs is supported

FlatFileColumn

A 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:

  1. A date/time format, declared by a leading D and followed by a date/timepattern as understood by the Java SimpleDateFormat class. Example: DyyyyMMdd
  2. A number format, declared by a leading N and followed by a number pattern as understood by the Java DecimalFormat class. Example: N000.00
  3. A padding format, declared by
    1. A leading number which declares the column width and
    2. Optional decimal point followed by a number which specifies the count of decimal digits and
    3. Optional following alignment code (l(eft), r(ight), c(enter) and
    4. Optional padding character. if not specified otherwise, output is left-aligned and padded with space characters. Example: 8, 10.2r0, 30c

Examples are:

  • 20
  • 3r0
  • DyyyyMMdd
  • N0000.00

FlatFileBean Example

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;
   }

}

Writing Flat-File-Beans

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();

Reading Flat Files

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();