• Post Reply Bookmark Topic Watch Topic
  • New Topic

File based persistent store with Java  RSS feed

 
Vijay Chouhan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!