在Java中锁定属性文件 [英] Lock Properties File in Java

查看:152
本文介绍了在Java中锁定属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java属性文件构造。一开始我读它来填充一个对话框,但我也给了用户改变对话框中的值并点击保存的能力。这使我调用属性文件的setProperty方法。现在,由于这个web应用程序可以存在于多个浏览器中,所有更改同一个文件,我希望能够在保存方法中锁定属性文件。我怎样才能做到这一点?我见过类似的问题引用FileLock,但我不确定这是否适用于属性文件结构。是每个setProperty一个不同的写?



谢谢!

解决方案

setProperty 只是更新一组属性,它不会写入文件。您必须调用 store(OutputStream out,String header) 方法实际写出文件到磁盘,你必须锁定文件的时间。

您可以使用 FileLock 来防止多个用户同时写入文件,但每次您的用户想要保存,您将需要从磁盘重新加载属性文件,更改属性,并重新写入文件,所有这些都不释放文件锁定,以确保没有过时的数据保存。


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.

这篇关于在Java中锁定属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆