• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to delete a Record from a Random Access File?  RSS feed

 
Ben Prequel
Greenhorn
Posts: 19
Chrome IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I was developing a small test application using Random Access File. I am attaching the source code with it. I want to delete a record from the file. I have searched similar threads
on Delete Record Random Access File. These threads suggest setting a flag as deleted against a record and not display it. I want to actually delete it. I tried to search the record and
tried to append a null record to that file pointer position. Nothing happens. Please suggest, whats the correct way to do it? Please find the source code below.



Here is the code for my main program where different operations are present.



Please suggest the correct way, in order to delete a record from a particular location in Random Access File?

Thanks,
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it, you can't.

You can, however, write a new file (named "temp.txt", for example) without the record. then you rename it to the original file name, effectively "deleting" it from the original file.
 
Ben Prequel
Greenhorn
Posts: 19
Chrome IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred,

It means, every time, i delete the record, i need to write it to a new file. What if we try to insert a blank record at that position?
Will that do? Please have a look at my code, i am trying to write a blank record, but it doesn't show any effect.

Thanks,
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Prequel wrote:What if we try to insert a blank record at that position? Will that do?

That would depend on your specific requirements. If having a blank record is allowed, then yes - assuming you can get it to work.
 
Paul Clapham
Sheriff
Posts: 22838
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back in the early stone age of computing, when random-access files were the hottest technology, we solved this problem by requiring each record to have a "deleted" flag as one of their fields. Just as your research suggested, in fact. Then deleting a record was as simple as setting the "deleted" flag to some particular code indicating the record was deleted.

That's because a random-access file consists of an array of bytes on a disk. You can't delete a byte from an array any more than you can delete the number 12 from the integers.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!