在实现过程咱我们可以通过ResourseBoundle来对properties属性文件进行读操作,但如果我需要在系统运行过程中,对其某配置项的值进行更改,并刷新到内存,该如何进行。

解决方案 »

  1.   


    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);
        }
      }}
      

  2.   

    注意:重新加载文件时要同步,上面的因为Properties本身是线程安全的所以不用同步
      

  3.   

    // 不断监控文件
                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!");
                    }
                }
      

  4.   

    public FileMonitor() {
                // 放到后台,与本程序脱离
                setDaemon(true);
                start();
            }
      

  5.   

    properties.clear();
            properties.load(new FileInputStream(configureFile));
      

  6.   

    可以考虑把properties当普通文件来处理.不就是读写文件吗?
      

  7.   

    class A{
    private File file;
    private Properties property=new Properties();
    。。}
    注意:重新加载文件时要同步,上面的因为Properties本身是线程安全的所以不用同步
    -------------------------------------------------
    没搞懂上面的properties为什么是线程安全的?
      

  8.   

    properties是从hashtable继承出来的,因此里面的所有方法都是线程安全的,有空研究研究JDK的代码就知道了