Forums Register Login

NX: Writng String[] to datafile

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
rubbery bacon. crispy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 671 times.
Similar Threads
Reading in db file
character coding problem
B&S: Data File Format
Writing back cache to the db file
I/O Misunderstanding (beta data file)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:10:57.