• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX:[URLyBird]Read and write record with fixed size

 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • 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
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic