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.
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.
Data in a fixed-width text file is arranged in rows and columns, with one entry per row. Each column has a fixed width, specified in characters, which determines the maximum amount of data it can contain. No delimiters are used to separate the fields in the file.
ParseFile is a local representation of a file that is saved to the Parse cloud. The workflow is to construct a ParseFile with data and optionally a filename. Then save it and set it as a field on a ParseObject .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With