Doing this with a text file is going to be dangerous, I think, no matter what method you use. There are going to be concurrency issues unless you're very careful and have complete control of the environment. And even if you have these guarantees today doesn't mean you'll still have them at some later date.
A proper database using transactions is really the right way to handle this, I think.