• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

reaing / writting to / from files

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need to use a file to save info about reservations. if someone makes a reservation I would add the following info to the file:

reservationNumber, flightNumber, numberOfSeats

now, i'm going to be searching this file a lot and reading info about various reservations. I'm also going to be deleting entries from it based on the reservationNumber (ie, cancel reservation 001 would mean search through file for reservation 001 then delete that line).

What sort of file should I use for this? Does anyone have any code to get me started?

J
 
Master Rancher
Posts: 5112
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like you need a database.
A flat file ie one with bytes 0 thru n would be difficult to add and delete entries to/from without continually reading and writing the WHOLE file.
Sometimes you can use RandomAccessFiles to allow update in place where the new data exactly replaces the old, ie it is same size. But this wouldn't support adding and deleting entries.

Perhaps you need a group of files with an index (sort of a roll-your-own database).
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this for school or a learning exercise of some kind? You might get by with loading the whole file into memory at program start, making changes in memory and rewriting the whole file to disk again when you're done. It's easy to find, add and remove entries in a collection in memory.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic