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
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.
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
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
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?
Joined: Nov 19, 2003
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.