java如何实现动态更新properties属性文件 在实现过程咱我们可以通过ResourseBoundle来对properties属性文件进行读操作,但如果我需要在系统运行过程中,对其某配置项的值进行更改,并刷新到内存,该如何进行。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 class A{private File file;private Properties property=new Properties();public A(){ init(); Thread t=new Thread(new Listener()); t.setDaemon(true); t.start();}private void init(){ property.load(new FileInputStream(file));//重新加载文件}class Listener implements Runnable{//扫描文件改动 private long modifyTime=file.lastModified(); public void run(){ while(true){ long l=file.lastModified(); if(l>modifyTime){ modifyTime=l; init(); } Thread.sleep(100); } }} 注意:重新加载文件时要同步,上面的因为Properties本身是线程安全的所以不用同步 // 不断监控文件 while (m_bIsMonitor) { try{ // 判断文件是否已修改了,若已修改过 if (nModifiedTime != f.lastModified()) { nModifiedTime = f.lastModified(); m_bIsModified = true; //遍历HashMap的key并根据key从配置文件中更新HashMap it = m_hm.keySet().iterator(); file = new FileInputStream(m_szFP); properties = new Properties(); properties.load(file); while(it.hasNext()){ szKey=(String)it.next(); szVal=properties.getProperty(szKey); if (szVal != null) { szVal = szVal.trim(); } m_hm.put(szKey, szVal); } file.close(); } }catch(IOException ioe){ System.out.println(this.getClass()+":syscfgfile path error!"); ioe.printStackTrace(); } // 停顿间隔 try { sleep(m_nInterval); } catch (InterruptedException e) { e.printStackTrace(); System.out.println(this.getClass()+":Thread sleep error!"); } } public FileMonitor() { // 放到后台,与本程序脱离 setDaemon(true); start(); } properties.clear(); properties.load(new FileInputStream(configureFile)); 可以考虑把properties当普通文件来处理.不就是读写文件吗? class A{private File file;private Properties property=new Properties();。。}注意:重新加载文件时要同步,上面的因为Properties本身是线程安全的所以不用同步-------------------------------------------------没搞懂上面的properties为什么是线程安全的? properties是从hashtable继承出来的,因此里面的所有方法都是线程安全的,有空研究研究JDK的代码就知道了 求助 关于java中BASE64解码算法 一个本机RMI测试例子程序 没有可用分了,大家帮帮忙。。。WEB.XML问题, 在APPLET中如何使用LookAndFeel jar文件怎么运行啊! run time session的问题 下载的源码导入eclipse后出错:****refers to the missing type Elist JPanel add 的代码位置问题 关于JList和JScrollPane 想用JAVA提取位图的数据,请知道的人指个路,谢谢
class A{
private File file;
private Properties property=new Properties();public A(){
init();
Thread t=new Thread(new Listener());
t.setDaemon(true);
t.start();
}private void init(){
property.load(new FileInputStream(file));//重新加载文件
}class Listener implements Runnable{//扫描文件改动
private long modifyTime=file.lastModified();
public void run(){
while(true){
long l=file.lastModified();
if(l>modifyTime){
modifyTime=l;
init();
}
Thread.sleep(100);
}
}}
while (m_bIsMonitor) {
try{
// 判断文件是否已修改了,若已修改过
if (nModifiedTime != f.lastModified()) {
nModifiedTime = f.lastModified();
m_bIsModified = true;
//遍历HashMap的key并根据key从配置文件中更新HashMap
it = m_hm.keySet().iterator();
file = new FileInputStream(m_szFP);
properties = new Properties();
properties.load(file);
while(it.hasNext()){
szKey=(String)it.next();
szVal=properties.getProperty(szKey);
if (szVal != null) {
szVal = szVal.trim();
}
m_hm.put(szKey, szVal);
}
file.close();
}
}catch(IOException ioe){
System.out.println(this.getClass()+":syscfgfile path error!");
ioe.printStackTrace();
} // 停顿间隔
try {
sleep(m_nInterval);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(this.getClass()+":Thread sleep error!");
}
}
// 放到后台,与本程序脱离
setDaemon(true);
start();
}
properties.load(new FileInputStream(configureFile));
private File file;
private Properties property=new Properties();
。。}
注意:重新加载文件时要同步,上面的因为Properties本身是线程安全的所以不用同步
-------------------------------------------------
没搞懂上面的properties为什么是线程安全的?