• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random Access File  RSS feed

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a read/write, RandomAccessFile that I will use to store several student ID and gpa score. What I don't understand is how I should go about retrieving a particular set of data.

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For seek you are looking for the offset, which I think is in bytes, from the start of the file. That is index × record length. I think your record length is 12 so you would find the 5th element at 4 × 12 = 48. At least I think that is how it works.

Make the index 0‑based like in arrays. That assumes you are using 0, 1, 2, 3 etc as IDs. If you are using anything else, you might be better mapping the ID to the score with a Map.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, in order to use RAF effectively, you must know what type of file information you are dealing with, in order to rw? This doesn't seem like a very sophisticated class to use since you must know exactly where and what you are looking for in order to locate any data.
What if the student ID's are not all of the same length? Thanks Campbell.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, "not sophisticated" is a good description. If you're going to use a random-access file to store records, then making all the records be the same length is almost compulsory. Otherwise your design is going to have to do something like building an index to point to the beginning of each record, and dealing with what happens when updating a record changes its length, and so on ad nauseam.

If you look at how databases are implemented, you might find that at the very bottom of the implementation is something like this. But the database-builders have built levels of abstraction on top of the basic random-access files. Back in the dark ages of computing was ISAM -- with fixed-length records -- and then VSAM, a huge technological leap forwards. Nowadays all of that is ancient history, though.
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of thoughts. I don't think business logic belongs inside your finally block, that is usually used for freeing up allocated resources (e.g. calling close()). You are allowing the user to specify an int for the ID which could create a huge RAF depending on what they enter, assuming you are using the ID as part of the seek() into the RAF. Seems like there are two alternatives: assign the ID's sequentially, or, searching the RAF sequentially for the first occurrence of the ID.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!