• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deleting data form a file?

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
great makes perfect sense

thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic