class读取properties方式

来源:互联网 发布:帝国cms手机站教程 编辑:程序博客网 时间:2024/05/22 01:34

class读取properties方式

1)

             String p1 = Thread.currentThread().getContextClassLoader().getResource("").getPath()+"\\database.properties";
             Properties p2 = new Properties();
             FileReader fr = new FileReader(new File(p1));
             p2.load(fr);

2)

            Properties pro = new Properties();//database.properties
            InputStream in = this.getClass().getClassLoader() .getResourceAsStream("database.properties");//默认是classpath范围的资源
            pro.load(in);

3.通过apachecommons-configuration组件

            Configuration  config = new PropertiesConfiguration("database.properties");

            config.setReloadingStrategy(new FileChangedReloadingStrategy());   //使用自动加载      

      config.setProperty("colors.background", "#000000);                                                        config.save(); 
      config.setAutoSave(true); //设置属性后自动保存
            colors.pie = #FF0000, #00FF00, #0000FF
      String[] colors = config.getStringArray("colors.pie"); //你不用手动拆分字符串,可以直接作为数组返回:
            另外一种作法, 你可以通过多行的同名键值对,来返回一组值。
      colors.pie = #FF0000;      colors.pie = #00FF00;      colors.pie = #0000FF;
   

0 0
原创粉丝点击