Eclipse-Plugin在运行时写入属性

时间:2012-05-21 14:22:35

标签: properties eclipse-plugin

进入我的项目插件根我有文件 my.properties

#comment
property=hello

下面你可以找到myWizard的init

@Override
public void init(IWorkbench arg0, IStructuredSelection arg1) 
{
   .... CUT ....
   Properties prop = new Properties();
   try 
   {      
       prop.load(this.getClass().getClassLoader().getResourceAsStream("my.properties"));
       System.out.println("prop extract = " + prop.getProperty("property"));
   } 
   catch (IOException e) 
   {
       e.printStackTrace();
   }
}

这很好用。

现在我需要在这个文件中添加一些属性,我尝试使用这段代码,但是它不起作用了 只打印了一个属性值为“hello”

try 
{
    Properties prop = new Properties();
    prop.load(this.getClass().getClassLoader().getResourceAsStream("my.properties"));
    prop.setProperty("property2", "hello2");
    prop.store(new FileOutputStream((this.getClass().getClassLoader().getResource("my.properties")).getFile()), "add a runtime");

    //Here reload my properties file and get his properties
    //because I want to check if the file has been overwritten or not
    Properties prop2 = new Properties();
    prop2.load(this.getClass().getClassLoader().getResourceAsStream("my.properties"));
    Enumeration props2 = prop2.elements();
    while (props2.hasMoreElements()) 
    {
       String object = (String) props2.nextElement();
       System.out.println("property = " + object );
    }
} 
catch (IOException e) 
{
    e.printStackTrace();
}

有什么帮助吗? 感谢

0 个答案:

没有答案