Get your CodeRanch badge!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Deleting data form a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Deleting data form a file?" Watch "Deleting data form a file?" New topic
Author

Deleting data form a file?

Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi all:
How can I delete data from a file? I am using RandomAccessFile to read a database file, then I obtain its FileChannell object. It provides methods to write and read, but not override or delete byts.


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Denis Spirin
Ranch Hand

Joined: Mar 22, 2004
Posts: 72
Hi
As far as I know FileChannel has write methods. In my instructions it was specified that to delete record one need to rewrite corresponding field in record (0x8000 - live record, 0 - deleted one). There must be something in your instructions.


Regards<br /> <br />Ph.D.<br />SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD 2
Ben Zung
Ranch Hand

Joined: Mar 25, 2004
Posts: 109
I think it is arguable to use FileChannel for this assignment. RandomAccessFile has a file pointer with which you can overwrite the status byte (int value = 255, byte -1, in B.S assignment) with raf's writeByte method. And that is the only thing you need to delete the record.
Just my 2 cents.
Bing
Richard Everhart
Ranch Hand

Joined: Nov 19, 2003
Posts: 54
Hi Hanna,
Here's how I'm doing it. My instructions say that each record has associated with it a deleted field so when I delete a record I don't actually remove it but instead I set its deleted field to '1', indicating that the record has been deleted. My instructions further state that I am able to reuse the fields for this record when I need to create a new one. So, in createRecord() I search the records (I use a cache to store records so I search it instead of the actual database file) until I find the first deleted record. I reuse this record's fields for the new record I'm about to create. If there are no deleted records then I add the new record at the end of the database file. Overwriting a record's field is pretty easy. Just get a byte[] the size of the field in question where each byte is initialized to 0x0 (a fancy way to say zero). Then use System.arraycopy() to copy the new field into this empty byte[]. Then write the previously empty byte[] over the field in question.
This is just one way to do things of course. Hope this helps.
Rich
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
i was wondering -

1) when you read the data in from the file to create a List (or Map) of records to be cached in memory, are you including the VALID byte as part of the record data?

I think so, would make things easier, but wondering what others were doing

2) also - I assume (using RAF) that it is possible to just overwrite one byte and not have to overwrite the entire file when updating - can someone confirm this for me?

thanks

LW
Richard Everhart
Ranch Hand

Joined: Nov 19, 2003
Posts: 54
Hi Laura,

I am creating a Map of records which includes the deleted flag. Even though this flag is not part of the database schema (it's not accounted for in the hearder section) it is still associated with a record. So, I think that it is natural to group the record with its deleted flag.

I assume (using RAF) that it is possible to just overwrite one byte and not have to overwrite the entire file when updating - can someone confirm this for me?


Yes, you can overwrite a single byte in a file using RAF or multiple bytes. You just need to seek() to the desired position in the file and then call one of RAF's many write() methods. Which one is up to you but you'll probably find write(byte[]) pretty helpful.

I hope this helps.

Rich
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
great makes perfect sense

thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deleting data form a file?
 
Similar Threads
can i delete a data of file
URLyBird Question
Delete data from random access file
deleting lines from file
How to create password protected file