日期:2014-05-17 浏览次数:20878 次
//加载文件,路径默认指向classpath Configuration config = new PropertiesConfiguration("test.properties"); //获取配置属性 String name=config.getString("name"); int age=config.getInt("age"); String friend=config.getString("girl.friend"); //还可以为键对应值为空的设置默认值 String friend=config.getString("girl.friend","angle"); //将规律的字符串转换成数组读取,如字符串:name_list=bob,angle,miqi String[] nameArray=config.getStringArray("name_list"); List<String> nameList=config.getList("name_list"); //当然既然可以将有规律的字符串转换成数组,就可以定义分隔符 //如规律字符串friend_list=bob|angle|king AbstractConfiguration.setDefaultListDelimiter('|'); String[] friendArray=config.getStringArray("friend_list"); List<String> friendList=config.getList("friend_list");
<?xml version="1.0" encoding="utf-8"?> <app> <name>appName</name> <version>appVersion</version> </app>
XMLConfiguration config = new XMLConfiguration("test.xml"); //获取文件内的属性,方法和PropertiesConfiguration相似 String appName = config.getString("app.name"); double version = config.getDouble("app.version"); //如果有中文乱码可以: PropertiesConfiguration conf = new PropertiesConfiguration(); conf.setEncoding("UTF-8"); conf.setFileName(fileName); conf.load();