Parsing a fixed-width formatted file in Java
I've got a file from a vendor that has 115 fixed-width fields per line. How can I parse that file into the 115 fields so I can use them in my code?
My first thought is just to make constants for each field like NAME_START_POSITION
and NAME_LENGTH
and using substring
. That just seems ugly, so I'm curious about better ways of doing this. None of the couple of libraries a Google search turned up seemed any better, either.
I would use a flat file parser like flatworm instead of reinventing the wheel: it has a clean API, is simple to use, has decent error handling and a simple file format descriptor. Another option is jFFP but I prefer the first one.
I've played arround with fixedformat4j and it is quite nice. Easy to configure converters and the like.
uniVocity-parsers comes with a FixedWidthParser
and FixedWidthWriter
the can support tricky fixed-width formats, including lines with different fields, paddings, etc.
// creates the sequence of field lengths in the file to be parsed
FixedWidthFields fields = new FixedWidthFields(4, 5, 40, 40, 8);
// creates the default settings for a fixed width parser
FixedWidthParserSettings settings = new FixedWidthParserSettings(fields); // many settings here, check the tutorial.
//sets the character used for padding unwritten spaces in the file
settings.getFormat().setPadding('_');
// creates a fixed-width parser with the given settings
FixedWidthParser parser = new FixedWidthParser(settings);
// parses all rows in one go.
List<String[]> allRows = parser.parseAll(new File("path/to/fixed.txt")));
Here are a few examples for parsing all sorts of fixed-width inputs.
And here are some other examples for writing in general and other fixed-width examples specific to the fixed-width format.
Disclosure: I'm the author of this library, it's open-source and free (Apache 2.0 License)