• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

File based persistent store with Java

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was thinking of implementing a file based persistent store in Java. I can write the contents of the Objects to a file on the disk. I can dump 100 objects (e.g., for 100 employees) to a file. However how do I read one of the object (for a particular employee) back?

For example if I want to change the data for an object, how can I retrieve the object and replace it in the file.

I hope my question makes sense. In terms of databse terms, Create is fine, but how do I perform Read/Update/Delete?

Couple of options that comes to my mind are:
- create a different file for each object
- retrieve the data for all objects, deserialize them and subsequently persist them again after performing the relevant operations

But both of them are not efficient and have drawbacks.
I hope my question makes sense. Will be thankful for any suggestions/pointers.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using object serialization, you'd have to read the whole collection in, update it and serialize the whole thing back out.
It is possible to create a low-level database using RandomAccessFile. I believe that article leaves some of the maintenance tasks to the reader.
It depends on your requirements. If you only have a few records and don't mind reading/writing the whole collection with every edit, serialization may fit the bill. If you have a simple database, rolling your own with RAF may work. Of course, the more complex the requirements, the more burdensome rolling your own becomes and you'd be better off looking at an embedded database like Berkeley DB or, for even more complex software, an embedded SQL db like Apache Derby.
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijay Chouhan:
Hi,

I was thinking of implementing a file based persistent store in Java. I can write the contents of the Objects to a file on the disk. I can dump 100 objects (e.g., for 100 employees) to a file. However how do I read one of the object (for a particular employee) back?

For example if I want to change the data for an object, how can I retrieve the object and replace it in the file.

I hope my question makes sense. In terms of databse terms, Create is fine, but how do I perform Read/Update/Delete?

Couple of options that comes to my mind are:
- create a different file for each object
- retrieve the data for all objects, deserialize them and subsequently persist them again after performing the relevant operations

But both of them are not efficient and have drawbacks.
I hope my question makes sense. Will be thankful for any suggestions/pointers.



Hi Vijay,

You could also consider creating an 'index' file, that would point you to a particular position in your datastore file\directory. You could potentially have several such 'index' files: say, by name, employeeId, etc. Also, unless you're looking @ a lot of files(day, over a 1000), I'd probably use a directory structure to store the data.
 
reply
    Bookmark Topic Watch Topic
  • New Topic