一般我们会把经常修改的内容写在xml中利于我们修改,维护信息
使用co信息mmons-configuration.jar读取个人认为很方便ConfigXML
ConfigXml类代码:
private static final String CONFIG_PATH = "config.xml";
private static Configuration config ;
static{
try {
config = new XMLConfiguration(CONFIG_PATH);
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
public static String getString(String infoLabel) {
return config.getString(infoLabel);
}
public static boolean getBoolean(String infoLabel) {
return config.getBoolean(infoLabel);
}
public static int getInt(String infoLabel) {
return config.getInt(infoLabel);
}
public static double getDouble(String infoLabel) {
return config.getDouble(infoLabel);
}
public static String[] getStringArray(String infoLabel) {
return config.getStringArray(infoLabel);
}
public static void main(String[] args){
System.out.println(getInt("cache.timeout"));
}
使用方法:
假如config.xml中有段内容为:
<students>
<sname>张三</sname>
</students>
读取方法只需要:ConfigXml.getString("students.sname");