views:

834

answers:

3

I've got a file from a vendor that has 115 fixed-width fields per line. What's the best way of parsing 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 if there's any other recommended ways of doing this. None of the couple of libraries a Google search turned up seemed any better either. Thanks

+5  A: 

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.

Pascal Thivent
I just wanted to follow up with a thanks for a pointer to Flatworm. It works like a champ and my whole team at work is now using it.
MattGrommes
@MattGrommes I'm glad to know you liked it. And thank you very much for the follow up, it's very much appreciated!
Pascal Thivent
+1  A: 

I've played arround with fixedformat4j and it is quite nice. Easy to configure converters and the like.

p3t0r
A: 

The Apache Commons CSV project can handle fixed with files.

Jherico