Saturday, February 09, 2008

Create Fixed-Width Files Easily

If you need to create fixed-width files, you may be interested in a fixed-width recordset builder I wrote for Groovy. It lets you create a set of fixed-width records with the following sort of code:
def builder = new FixedWidthRecordSetBuilder()
builder.TestData(){
record1(){
field1(startPosition : 0, width : 5, content : short_content)
field2(startPosition : 5, width : 10, content : long_content)
}
record2(){
field1(startPosition : 0, width : 5, content : short_content.reverse())
field2(startPosition : 5, width : 10, content : long_content.reverse())
}
}

builder.records.each { record ->
println record.formattedContent //or to a file
}

You can get the goods from my svn repository:
You can log in with the user guest. The user does not have a password.

The Groovy website has more on the general topic of builders in Groovy.

Enjoy!

No comments: