第一次用getProperty()读取配置文件后,配置文件会被缓存到内存中,当我用setProperty()修改配置文件后,再去取值,系统会去缓存中读取,取回来的还是没修改前的信息,所以请教:
(1)配置文件是通过什么方式缓存到内存中的,静态变量?
(2)如何实现修改配置文件后,立刻在内存中重新加载配置文件?或者去掉缓存中的信息

解决方案 »

  1.   

    你的Properties对象是什么时候读取的文件,这个对象放在哪个变量里,你都不知道吗?
      

  2.   

    //读取配置文件信息
    public String getPath(String filename, String strPath) throws IOException {
    InputStream inputStream = this.getClass().getClassLoader()
    .getResourceAsStream(filename);
    String savePath = "";
    Properties p = new Properties();
    try {
    p.load(inputStream);
    savePath = p.getProperty(strPath);
    } catch (IOException e1) {
    e1.printStackTrace();
    }finally{
    inputStream.close();
    }
    return savePath;
    }其他地方都是直接调的这个接口来读取配置文件
      

  3.   

    public void setPath(String filename, String propertyName, String value) throws IOException {
    InputStream inputStream = null;
    FileOutputStream outputStream = null;
    Properties p = new Properties();
    try {
    inputStream = this.getClass().getClassLoader().getResourceAsStream(filename);
    outputStream = new FileOutputStream(this.getClass().getClassLoader().getResource("/").getPath() + filename);

    p.load(inputStream);
          p.setProperty(propertyName, value).toString();
          p.store(outputStream, "Description");
          String reload = new Common().getPath(filename, propertyName);
    } catch (FileNotFoundException e2) {
    e2.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    inputStream.close();
    outputStream.close();
    }
    }
      

  4.   

    看一下这个文章:http://www.javaeye.com/problems/9776
      

  5.   

    写一个类,用一个静态变量保存property对象,她只在第一次访问时读取文件,之后都是直接访问,读取、写都是对这个属性操作
      

  6.   

    能说的具体点吗?如何用静态变量保存property对象?以后都是直接对该静态变量进行读写操作?
      

  7.   

      如何实现修改配置文件后,立刻在内存中重新加载配置文件?
         1>刷新下项目-主要要刷新下项目下的配置文件(如果在MyEcplise下看不到你的那个配置文件就没必要刷新了)
         2>load
      

  8.   

        如果在MyEcplise可以看到你那个配置文件,说明它是正打开的(你不刷新显示的还是以前的数据)