• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Writng String[] to datafile

 
Hugh Johns
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably a very simple solution to this.
I am implementing the upDateRecord() method in the Data class.
I am passing a String array holding the record fields to update, and using a Random-Access file stream to write the fields.
I have found a thread advising the use of writeByte(), and write(Byte[]).
I am converting each strings in the fields array to Byte[], then writing these to the datafile. Problem is the Byte[] to write has to be exactly the size dictated by the database schema to keep the structure of the database.
How is a string (shorter than say 32 bytes) which is converted to byte[] padded to 32 bytes ensuring no garbage is left in the field from a previous value. Is there another easier way of doing this simple task.
Hope the above long winded description makes sense.
 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, this is slightly more complicated than that; uou also have to ensure that the strings are not LONGER than they're supposed to be. You can do this in the client but encapsulating it in the Data class was my choice.
For strings too long I just used substring, for strings too short I converted to a string buffer and then created a loop within which I appended spaces.
You may want to cover this handling in your choices.txt file.
ms
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hugh,

How is a string (shorter than say 32 bytes) which is converted to byte[] padded to 32 bytes ensuring no garbage is left in the field from a previous value. Is there another easier way of doing this simple task.

If you just created your byte[] with a size equal to field length, the array is already intialized with 0x0.
But if you reuse it, you may use Arrays.fill() to clear a previous value.
Regards,
Phil.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic