📄 xmlconfig.java
字号:
package cn.js.fan.util;import org.jdom.*;import org.jdom.output.*;import org.jdom.input.*;import java.io.*;import java.net.URL;import cn.js.fan.util.XMLProperties;import org.apache.log4j.Logger;import cn.js.fan.util.StrUtil;import java.net.URLDecoder;public class XMLConfig { private XMLProperties properties; private String filePath; Logger logger; Document doc = null; Element root = null; String rootChild = ""; String encoding = "gb2312"; public XMLConfig(String filePath, boolean isRealPath, String encoding) { this.encoding = encoding; this.filePath = filePath; if (!isRealPath) { URL cfgURL = getClass().getClassLoader().getResource(filePath); this.filePath = cfgURL.getFile(); this.filePath = URLDecoder.decode(this.filePath); } File file = new File(this.filePath); logger = Logger.getLogger(XMLConfig.class.getName()); SAXBuilder sb = new SAXBuilder(); try { doc = sb.build(file); root = doc.getRootElement(); properties = new XMLProperties(file, doc); } catch (org.jdom.JDOMException e) { logger.error("XMLConfig:" + e.getMessage()); } catch (java.io.IOException e) { logger.error("XMLConfig:" + e.getMessage()); } } public void setRootChild(String rootChild) { this.rootChild = rootChild; } public Element getRootElement() { return root; } public String get(String name) { return properties.getProperty(name); } public int getInt(String name) { String p = get(name); if (StrUtil.isNumeric(p)) { return Integer.parseInt(p); } else return -65536; } public void set(String name, String value) { properties.setProperty(name, value); } public String getDescription(String name) { Element which = root.getChild(rootChild).getChild(name); if (which == null) return null; return which.getAttribute("desc").getValue(); } public boolean put(String name, String value) { Element which = root.getChild(rootChild).getChild(name); if (which == null) return false; which.setText(value); writemodify(); return true; } public void writemodify() { String indent = " "; boolean newLines = true; Format format = Format.getPrettyFormat(); format.setIndent(indent); format.setEncoding(encoding); System.out.println("XMLConfig.java writemodify:encoding=" + encoding + " filePath=" + filePath); XMLOutputter outputter = new XMLOutputter(format); try { FileOutputStream fout = new FileOutputStream(filePath); outputter.output(doc, fout); fout.close(); } catch (java.io.IOException e) { System.out.println("XMLConfig.java writemodify:" + e.getMessage()); } } public Element getRoot() { return root; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -