• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NX: Writng String[] to datafile

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic