Top.Mail.Ru
October 27th, 2003 - Java developers — LiveJournal
? ?

Java developers

October 27th, 2003
Image

11:12 pm - Imagepbrane - Simple File IO question...

I've got a program that reads a file that was the output of java.util.Properties.store(), and uses it as a simple text database while the program is running. My problem is that I want to have the program be able to edit this information, and save it back to disk while the program is running (and do this repeatedly [but still sequentially]).

This is easy, I know it is, but I can't seem to get it to work right. For example, I can read in the file just fine, and the program then has a nice Properties object to play with, which I modify with Properties.setValue or whatever, but the disk still has the original filename sitting there - what's the right way to modify it to reflect the changes in the in-memory object?

I was trying to do a simple file.renameTo(new File(oldfilename + ".old")), and then Properties.store(new FileOutputStream(oldfilename)) to recreate the file with the right new values, but this somehow doesn't seem to work - the renaming happens, but there's no new file. Also, store() seems to always be appending to my file if it's not empty - do I need to close() the output stream or something to make sure it just overwrites it?

I'm sorry if these questions are vague, but I have gazillions of lines of code, so showing you the exact code would be a pain in the ass, maybe even just a pointer to your favorite file IO tutorial (esp. if it in particular talks about Properties interactions, or if there's a better way to do simple key=value string based data saving/restoring from disk).

Thanks in advance for any help.
Powered by LiveJournal.com
Image