public static HashMap<String, String> ReadConfig() {
HashMap<String, String> map=new HashMap<>();
try {
SAXReader reader = new SAXReader();
Document document= reader.read(new File("config.xml"));
Element node = document.getRootElement();
@SuppressWarnings("unchecked")
List<Element> listElement = node.elements();
for (final Element e : listElement) {
map.put(e.getName(), e.getText());
}
} catch (Exception e) {
e.printStackTrace();
logger.info("读取配置文件错误"+e.getMessage());
}
return map;
} public static void WriterConfig(HashMap<String, String> map) {
try {
SAXReader reader = new SAXReader();
Document document= reader.read(new File("config.xml"));
Element node = document.getRootElement();
for (Entry<String, String> entry : map.entrySet()) {
node.addElement(entry.getKey()).setText(entry.getValue());
}
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File("config.xml")), "UTF-8"), format);
writer.write(document);
writer.flush();
writer.close();
} catch (Exception e) {
logger.info("写入配置文件出错"+e.getMessage());
e.printStackTrace();
}
}