0

I'm currently working on a random access file and I need to have an update method. This method deletes a record and replace it with a new record. I'm wondering how could I do that method.. I am required to use random access file.. but if there's simpler way to update a file created in a random access that would be great.any ideas would be of great help :) thank you for taking time reading this.

    public void updateSavingsFile(String file, int id){
            int i = 0;
            try{
                access = new RandomAccessFile(file, "rw");
                do{
                        access.seek(i*RECORD_SIZE);
                        access.getFilePointer();
                        if(access.readInt() == id){
                            //i'm into the record i wish to delete.. 
                            //how would i delete it?
                            //there is no method in random access that says delete 
                            //   or somethin..
                            break;
                        }
                        i++;
                    }while(access.getFilePointer()!=access.length());
                    access.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "error upadte: "+e);
        }
}

is it possible to overwrite a record? or any means like that?

  • What have you tried so far? Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). – DavidPostill Aug 17 '14 at 16:26
  • @DavidPostill i've created a random access file named "savings". it's working, I can seek through records, add new records.. the thing is I don't know how to delete a certain record. – Crievr Lumapac Aug 17 '14 at 16:29
  • Deleting of a record must be implemented by "updating" the record with data that indicates that there isn't a record any more. – laune Aug 17 '14 at 16:33
  • @laune is there any method in java raf that do updating? or somethin like that? – Crievr Lumapac Aug 17 '14 at 16:36
  • The answer is in your question. You *don't* delete the record, you overwrite (replace) it with the new record. After you've replaced it, the old information is gone so the old record is "deleted". – Erwin Bolwidt Aug 17 '14 at 16:45

0 Answers0