java - How to edit a entry sequenced enscribe file -


i need problem. looks stupid not resolved it. have entry sequenced file variable length records. need replace first 3 bytes xxx have rebuild whole file this.. problem getting changing length of records filling "nulls". that's why have no way know amount of bytes written record.

for example have file 3 records:

aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbb ccccc dddddddddddddd 

the file has rec attribute of 26 (equals length of second record). when execute program change first 3 letters, file remains (assume "n" "null character"):

aaaaaaaaaaaaaaaannnnnnnnnn bbbbbbbbbbbbbbbbbbbbbbbbbb cccccnnnnnnnnnnnnnnnnnnnnn ddddddddddddddnnnnnnnnnnnn 

how can change program want?

xxxaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbb ccccc dddddddddddddd 

this code (java)

enscribefile p_origin = new enscribefile(file); string first_record; byte buffer[];   //first, charge records , purge file content arraylist<byte[]> records = new arraylist<byte[]>(); buffer = new byte[et.getrecordlength()]; p_origin.open(enscribeopenoptions.read_write,enscribeopenoptions.shared); enscribefileattributes et = p_origin.getfileinfo(); while ( p_origin.read(buffer,et.getrecordlength()) != enscribefile.position_unused ) {     byte auxrecord[] = new byte[et.getrecordlength()];     system.arraycopy(buffer,0,auxrecord,0,et.getrecordlength());     buffer = new byte[et.getrecordlength()];     records.add(auxrecord); } p_origin.purgedata();  //second, modify first record first_record = new string(records.get(0)); first_record = "xxx" + first_record.substring(3); records.set(0,first_record.getbytes());  //third, rewrite records , close file iterator<byte[]> = records.iterator(); while( i.hasnext() )     p_origin.write(aux,et.getrecordlength()); //check note  p_origin.close(); 

note: can not add function last character before first null before write becouse previous null or nulls @ end of records possible , acceptable. example (remember "n" "null"):

aaaaaaaaaaaaaaaann bbbbbbbbbbbbbbbbbbbbbbbbbb cccccnn ddddddddddddddnn 

must equal after process:

xxxaaaaaaaaaaaaann bbbbbbbbbbbbbbbbbbbbbbbbbb cccccnn ddddddddddddddnn 

ok, found solution @ other forum. simple. method

p_origin.read(...) 

returns length of bytes did not know, simple save variable length before creating new record. changes code becomes:

enscribefile p_origin = new enscribefile(file); string first_record; byte buffer[];   //first, charge records , purge file content arraylist<byte[]> records = new arraylist<byte[]>(); buffer = new byte[et.getrecordlength()]; p_origin.open(enscribeopenoptions.read_write,enscribeopenoptions.shared); enscribefileattributes et = p_origin.getfileinfo(); int aux_len = p_origin.read(buffer,et.getrecordlength()); while ( aux_len != enscribefile.position_unused ) {     byte auxrecord[] = new byte[aux_len];     system.arraycopy(buffer,0,auxrecord,0,et.getrecordlength());     records.add(auxrecord);     aux_len = p_origin.read(buffer,et.getrecordlength()); } p_origin.purgedata();  //second, modify first record first_record = new string(records.get(0)); first_record = "xxx" + first_record.substring(3); records.set(0,first_record.getbytes());  //third, rewrite records , close file iterator<byte[]> = records.iterator(); while( i.hasnext() ) {     byte aux_byte[] = i.next();     p_origin.write(aux_byte,aux_byte.length); }  p_origin.close(); 

Comments