java - Checking for overlength fields in a fixed-position text file -
java - Checking for overlength fields in a fixed-position text file -
i have fixed width text file need parse. each line constitutes record, 1 or more fields occupying fixed position in line. way have programme set parses each line using parsing strategy depending on record type (the first 4 digits of line determine record type. parsing strategy series of substrings grab fields need, , place them field object. each field object has name (e.g. currency code) , info contains.
these field objects placed record object, has name , list of fields.
i figure out way determine if have overlength fields in given record. (e.g. if country supposed 5 characters long column 40-44 , set in 8 character string programme should able handle that).
this portion of code determines behavior , executes it:
string line; seek { // hold "current" record count line = in.readline(); for(; line != null; line = in.readline()) { record rec = context.executebehavior(line); transaction.insert(rec); // insert record current transaction } }
the executebehavior() line executes particular strategy's parse() method.
this parsebehavior interface, classes implement if want parse stuff
public interface parsebehavior { public record parse(string line); }
and here's illustration record
public class parserecord implements parsebehavior { public record parse(string line) { arraylist<field> recordset = new arraylist<field>(); string testflag = line.substring(11, 12); field testflagfield = new field(testflag, "test flag"); recordset.add(testflagfield); string location = line.substring(90, 125); field location = new field(location, "location"); recordset.add(location); record record = new record(recordset); homecoming record; } }
i guess it's not java question per se, more of design question. happen using java seek , solve problem.
thanks help!
edit here illustration record:
1220 t3050 yale street endwell ny 13760
the 1220 record type, t test flag occupies 1 character, address line 1 occupies 25 characters, city occupies 15 characters, , zip code occupies 10 characters. there fair amount of blank space work with.
java parsing
Comments
Post a Comment