View Single Post
  #4 (permalink)  
Old 07-29-2007, 09:06 PM
brianhks brianhks is offline
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
Quote:
Originally Posted by levent View Post
I think this should be possible with random access files.
It is possible but difficult. For example lets say you have the following file of names:
George Bush
Kermit the Frog
Bill Clinton
Mike Young
Skooby Doo

Now you wish to delete Bill Clinton. The name "Bill Clinton" takes up 12 bytes right in the middle of the file. You cannot just remove them like in an editor. You can blank them out but, in order to remove them you must rewrite the file from at least the beginning of Bill Clinton to the end.

For a small database you can read the entire thing into memory and write it back out when you have finished manipulating it.

Or you can use fixed length records in the file so that every name takes up 20 bytes (or whatever). This will still leave holes in the middle of the file that you have to keep track of and fill back in when inserting data.

The only two viable choices are to read the whole lot into a memory structure and write it out when done or to use a real database such as hsqldb (I advocate hsqldb because I've used it and it is delightfully easy).
Reply With Quote