• Post Reply Bookmark Topic Watch Topic
  • New Topic

java I/O  RSS feed

 
ivan gonzales
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a simple database using RandomAccessFile...can anyone help me with this?
the program should implement the ff. functions:
1. insert a record
2. delete a record
3. update a record
4. query a record
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual first question ... is this homework? Guessing it is, I'll hint at some things to look at rather than toss out code.
You can use random access files for this kind of thing if you have fixed length records. So it's good for storing identical structures, but not appropriate for handling plain text files with lines of different length.
With that in mind, let's think about 10-byte records.
What is the starting byte for record "n"? Files start at byte zero, so it's something like (n-1)*10. Try a few different record numbers and see if the starting bytes (offsets) look right.
How do we read record 5? Compute the starting byte and read a record length worth of bytes.
How do we update record 5? Go to the same starting byte and overwrite what's there.
Think about Insert and Delete some more. What would have to happen to the records after the one you insert or delete?
You're more than welcome to start coding some of this and post your code in progress if you run into trouble. Hope that helped!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!