Reminder: we have moved to GitHub .
We have two branches: 3_4_branch which remains JDK 1.5 and master/4_x which is JDK8. New development will be limited to master.
January 6, 2019: FlatPack 4.0.4 is released, see the change log
FlatPack came out of the frustration of having to mix file parsing logic with business logic.
FlatPack on SourceForge: a Java (1.8+) flat file parser that handles CSV (including values containing ','), fixed length and custom delimiters. The formats can be configured in XML, it is fast and released under Apache license 2.0.
Starting at release 3.1, FlatPack introduces exporters via the writer package. This mechanism allows you to export a DataSet to a fixed length or delimited format. A big thank you to Dirk and Holger from the Mule project for the initial code contribution. FlatPack should integrate nicely with Mule, the open source choice for integration and SOA.
Substrings in a fixed width parse can be daunting to deal with when trying to analyze what existing code is doing, and what about when you have no comments...
We also provide delimited file parsing ; works with any delimiter / qualifier, multiline records, delimiter or qualifier allowed in column value.
A manual is available as Word document or a PDF .
FlatPack is part of the ObjectLab Kit family.
It parses or creates Delimited files (e.g. CSV) or Fixed Width 'files'.
There is more to it than one could imagine at first sight:
Basic Steps
// Obtain the proper parser for your needs: basic CSV Parser with column Header Parser parser = CsvParserFactory.newForwardCsvParser(new FileReader("DataFile.csv")); //obtain DataSet StreamingDataSet ds = parser.parseAsStream(); while (ds.next()){ //loop through file Record record = ds.getRecord(); // immutable object, you can give it to a thread. String value = record.getString("mycolumnName"); // columns name are case INsensitive }
//Obtain the proper parser for your needs Parser parser = DefaultParserFactory.getInstance().newDelimitedParser( new FileReader("map.fpmap.xml"), //xml column mapping new FileReader("DataFile.txt"), //txt file to parse ',', //delimiter '"', //text qualfier false); //ignore the first record (may need to be done if first record contain column names) //obtain DataSet DataSet ds = parser.parse(); while (ds.next()){ //loop through file ds.getString("mycolumnName"); }
//Obtain the proper parser for your needs Parser parser = DefaultParserFactory.getInstance().newDelimitedParser( new FileReader("DataFile.txt"), //txt file to parse ',', //delimiter '"'); //text qualifier //obtain DataSet DataSet ds = parser.parse(); while (ds.next()){ //loop through file ds.getString("mycolumnName"); }
//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 to parse //obtain DataSet DataSet ds = parser.parse(); while (ds.next()){ //loop through file ds.getString("mycolumnName"); }
final String msg = "data data2 data3 data4" //Obtain the proper parser for your needs Parser parser = DefaultParserFactory.getInstance().newFixedLengthParser( new FileReader("map.fpmap.xml"), //fixed with column map to bind col names new StringReader(mag)); //data to parse //obtain DataSet DataSet ds = parser.parse(); while (ds.next()){ //loop through file ds.getString("mycolumnName"); }
Slf4j is utilized to capture logging information that occurred during a parse. By default, this information will be logged to the console. SLF supports the following loggers; log4j, jcl, nop, and jdk1.4. Please go to http://www.slf4j.org to download the proper jar for your logging preference. Here is a typical SLF4j setup: SLF-api-XXX.jar + SLF-MyLoggerPreference.jar + MyLogger.jar (lo4j.jar for example) Here are the steps that would need to be taken for log4j: 1. Install log4j.jar 2. Setup log4j.properties or log4j.xml 3. Install SLF-api-XXX.jar 4. Install SLF-log4j12-XXX.jar