Lock Properties File in Java
I am using the java properties file construct. At the beginning I read it to populate a dialog, but I also give the users the ability to change the values in the dialog and click save. This开发者_开发问答 causes me to call the setProperty method of the properties file. Now, since this webapp can exist over multiple browsers, all changing the same file, I want to be able to "lock" the properties file whenever I am in the "save" method. How can I accomplish this? I've seen similar questions refering to FileLock, but I am unsure if this applies to the properties file construct. Is each "setProperty" a different write?
Thanks!
setProperty
merely updates the set of properties, it does not write the file. You have to call the store(OutputStream out, String header)
method to actually write out the file to disk, and that is the time where you'll have to "lock" the file.
You can use FileLock
to prevent multiple users from writing to the file at the same time, however, each time your user wants to save, you will need to reload the property file from disk, change the property, and write the file back again, all without releasing the file lock, to make sure no stale data is saved.
精彩评论