Random Access File Adding & Searching Sequentially (with GUI)

full code image

(Pre-note: for a random access file to be random, you would need random access, so sequentially adding and searching is not treating it as a truly random access file, but for the mastery aspects 1 and 2, it's possible that it could count, since the way I get you to do it here, you are still using the seek() method as it stipulates.)

In the basics example, we simply wrote a couple of UTFs and then read them in. We could in fact use this straightforward approach to save and retrieve information. But it wouldn't be the most efficient way, even for a seemingly inefficient sequential search. And if we ramp up our efforts all the way to using direct access (and hashing) rather than the sequential approach, we need to be a bit more sophisticated and use the seek() method anyway.

So for the seek() method to be useful, we will need uniform record sizes, rather than writing one UTF after the other, butt-ending one to the next. So we will come up with a record size, and multiply a counter by that size to place the pointer. (Remembering that the pointer is not something we see in our code necessarily; rather it is just what keeps track of where we are in the file, ready to read or write. We will also make a MAXNUMBERRECORDS attribute which will be useful for looping though the file in our search.

Setting Things Up

And in our myInitComponents() method, we'll loop through the space that will be required for our file - achieving this by writing "" the number of records x the record length of one record.

Note that we'll need a global counter variable for keeping track of how many things have been added. And note that the RandomAccessFile variable will also have to be declared globally, though we don't call the constructor until inside our init method.

 14     private RandomAccessFile raf;
 15     private final int MAXNUMBERRECORDS = 10;//A Final "Constant", so it cannot be changed.
 16     private int recordSize = 20; //This will allow the seek to calculate exactly where to go to; see line 22.
 17     private int counter = 0;//For keeping track of how many things already added.
 19     private void myInitComponents() {
 21             raf = new RandomAccessFile("first raf 3.txt", "rw");
 22             for (int i = 0; i < MAXNUMBERRECORDS * recordSize; i++) {//So to beginning of each record.
 24                     raf.writeUTF("");//Initializing
 28             }
 32     }

(note that in these diagrams, the error handling is not shown, so as to keep things more clear.)

Adding Records

To add records, we will calculate the starting of the next record, with counter * recordSize. Do note that where the records begin is simply where we define them as beginning via our own declaration of recordSize; it's not like there's some sort of hard wired division between records. We simply say that the records start ever 20 UTFs, and so that's where they start simply because that's where we start writing and so also reading.

Note that our two UTFs will get written side by side, since after writing the first UTF, the pointer will be at the end of it, and that's where it will start writing the second UTF. That will be handy when searching, because after having found the university we are looking for, the pointer will be right there where the date is.

And do note that the counter must be ++ed at the end, so that the next Add button click will result in the correct seek() calculation.

276     private void addSequentiallyButtonMouseReleased(java.awt.event.MouseEvent evt) {                                                    
278             //  ADDING TO THE RAF. Done sequentially in this program, each time the Add button is pressed.
279             raf.seek(counter * recordSize);//To find the beginning of the record being added.
280             raf.writeUTF(addUniversityTextBox.getText());//Writes to the file the String entered in the textbox.
281             //Note that the writeUTF moves the pointer as well to the end of what it just wrote.
282             raf.writeUTF(addDateTextBox.getText());
283             addUniversityTextBox.setText("");//Just to clear the entry textboxes.
284             addDateTextBox.setText("");
285             counter++;//Can't forget to do this so that the next addition will start at the right place.
288     } 

Searching Records

You can read through the comments for what is going on and why. But it's basically looking through one record at a time, and it knows what is the start of the next record by doing the i*recordSize calculation each iteration through the loop. And when it gets the record which has the university we're looking for, then the date field will be the next UTF ready to be read.

290 private void searchSequentiallyButtonMouseReleased(java.awt.event.MouseEvent evt) {                                                       
291         //  SEARCHING THE RAF. Done the old fashioned way in this program; sequentially.
292         boolean found = false;
293         int i = 0;
294         while(!found && i < MAXNUMBERRECORDS){//So the while ends once it's found, or if get to the end of the file.
296                 raf.seek(i * recordSize);//Moving the pointer to 0, then, for this file, 20, and 40, and so on.
297                 String universityNameRead = raf.readUTF();//See what's at that record, and...
298                 if(universityNameEnteredTextBox.getText().equals(universityNameRead)){ //...compare it to what we're look
299                     //If we find the university we're looking for...
300                     //We display the date, which will be the next UTF that was written:
301                     dateFoundTextBox.setText(raf.readUTF());
302                     found = true;//So that the loop ends.
303                 }
304                 i++;//So that throughout the loop, we keep on looking at the next record.
308         }
309         if(!found){
310             dateFoundTextBox.setText("Sorry that university is not scheduled.");// :-(
311         }
312     }

Animation of Adding:

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player