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
You can use
\t+
as your delimiter.Try Something like
Here is a basic implementation I use:
I've played arround with fixedformat4j and it is quite nice. Easy to configure converters and the like.
The Apache Commons CSV project can handle fixed with files.Looks like the fixed width functionality didn't survive promotion from the sandbox.
If your string is called
inStr
, convert it to a char array and use theString(char[], start, length)
constructor