Mar
27
Announcing FlatPack 3.2 | Java Delimited / Fixed File API
ObjectLab and Paul Zepernick (me) are proud to announce Flatpack 3.2. Flatpack serves as a Java tool box for writing and reading fixed width and delimited files. It is a very mature API that has been in existence since 2005.
FlatPack is currently used in the Apache Camel project and Apache ActiveMQ just to name a couple.
For those not familiar with FlatPack, I have provided a couple quick examples below. Column names in the flat files can be bound via a header record in the file, or through an XML mapping.
Example of reading a delimited file
Parser parser = DefaultParserFactory.getInstance().newDelimitedParser(
new FileReader("DataFile.txt"), //txt file or String to parse. Can Take any Reader
',', //delimiter
'"'); //text qualifier
//obtain DataSet
DataSet ds = parser.parse();
while (ds.next()){ //loop through file
ds.getString("mycolumnName");
}
Example of reading a fixed width file
//Obtain the proper parser for your needs
Parser parser = DefaultParserFactory.getInstance().newFixedLengthParser(
new FileReader("map.fpmap.xml"), //fixed with column map
new FileReader("DataFile.txt")); //txt file or String to parse. Can take any Reader
//obtain DataSet
DataSet ds = parser.parse();
while (ds.next()){ //loop through file
ds.getString("mycolumnName");
}
Please check out the FlatPack website for more examples and complete documentation, or download now from Sourceforge.
