• 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:[URLyBird]Read and write record with fixed size

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,every one
In my assignment,there is a section "Database schema".It describe some information about field,such as "name->field length(64)->description..."
As it has given field length,I want to use a method read and write those fields so as to return each record.
I attempt to write it as a static member of dataSchema class,

Please each professionals comment this.
Is it right to consider that?
Regards,
Richard
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,
I do not understand why you are using a while loop. You should be able to read the required number of bytes with a call to one method in whatever class you are using to read from the file.
Even if you did want a loop to read the bytes from disk, you should only need to do one conversion from the array of bytes into the format you are returning.
You should be able to convert directly from an array of bytes into a String without going through a StringBuffer.
Regards, Andrew
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be able to read the required number of bytes with a call to one method in whatever class you are using to read from the file.
Um, provided that "whatever class" implements the DataInput interface as indicated, yes. But other stream and channel types often do require a loop to ensure that the desired quantity is actually read. (Well, in the case of FileChannel, the loop may not realy be necessary, but the API fails to guarantee this.) In general you can't guaranteed that a read() will read all the bytes you requested, and you may need to loop until the total is reached. (Even though in many/most cases you loop exactly once.) However for a DataInput implementation, there's another method which does the looping for you...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic