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.通过apache的commons-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;