最近在开发中用的比较多的是xml和properties这两种配置的格式文件,而yml暂时我还真的很少用,就学习spring Boot的时候用过,在现在公司开发小的工具程序都基本上用xml和properties。
但是现在感觉自己更倾向于用properties,但最近也还是在用xml写小程序,可能是习惯来得快?
接下来就写一下xml和properties的区别?也算是2018年最后一篇文章日志了,感谢现在努力的自己
格式写法?
xxx.xml文件格式
xxx.properties文件格式
从这两种图,可以看出xml的结构比properties清晰(yml也是),而properties文件要比.xml文件结构要简单一些。
可能是xml采用树形结构,而properties采用的是键值对的形式?
下面写下读取这配置文件内容的代码吧(代码比较乱勿喷)
- 读取properties文件的代码实现
package com.glj.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Properties; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; public class ConfigUtil { private static final Logger log = Logger.getLogger(ConfigUtil.class); private static Properties prop = null; public ConfigUtil(InputStream is) { prop = new Properties(); try{ BufferedInputStream bis = new BufferedInputStream(is); InputStream in = bis; prop.load(in); }catch (Exception e){ log.error("读取"+prop+"配置文件错误!",e); } } public static void init(String cfgPath) { String realPath = ""; if(StringUtils.isNotBlank(cfgPath) && new File(cfgPath).exists()) { realPath = cfgPath; }else { String defExistsPath = ClassLoader.getSystemResource("").getPath()+"cfg.properties"; realPath = new File(defExistsPath).exists()? defExistsPath: "";; } try{ InputStream is = StringUtils.isNotBlank(realPath)? new FileInputStream(realPath): ConfigUtil.class.getClassLoader().getResourceAsStream("cfg.properties"); log.info("读取配置文件:"+realPath); new ConfigUtil(is); } catch (IOException e1) { log.error("读取配置文件出错"); } } public static String getProperty(String propertyName) { if(prop == null) { ConfigUtil.init(""); } if("".equals(propertyName) || null==propertyName) { return ""; } return prop.getProperty(propertyName); } public static void main(String[] args) { // String path = ClassLoader.getSystemResource("").getPath()+"log4j.properties"; // ConfigUtil.init(path); // System.out.println(ConfigUtil.getProperty("log4j.rootLogger")); System.out.println(ConfigUtil.getProperty("mysql.className")); } }
- 读取xml文件的代码实现(基于DOM4J实现方法)
更多的方法可以到这个链接学习:https://www.cnblogs.com/lingyao/p/5708929.html
package com.glj.util; import java.util.Map; import org.dom4j.Document; /** * Xml文件操作接口 * @author Jiomer * */ public interface XmlSupport { /** * 创建xml文件 * @return */ public Document createDocument(); /** * 读xml文件 * @param file * @return * @throws Exception */ public Map readDocument(String file)throws Exception; /** * 写xml文件 * @throws Exception */ public void writerDocument() throws Exception; /** * 更新xml文件 */ public void updateDocument() ; } package com.glj.util; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.util.HashMap; import java.util.List; import java.util.Map; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class XmlSupportImpl implements XmlSupport { public Document createDocument() { // TODO Auto-generated method stub Document document = DocumentHelper.createDocument(); Element rootElement = document.addElement("root"); Element uploadFileElement = rootElement.addElement("uploadFile"); Element path = uploadFileElement.addElement("path"); path.setText("/Upload/"); Element size = uploadFileElement.addElement("size"); size.setText("10240"); Element type = uploadFileElement.addElement("type"); type.setText("zip,rar,png,jpg,gif"); return document; } public void writerDocument() throws Exception { } public Map readDocument(String file) throws Exception{ // TODO Auto-generated method stub Map map = new HashMap(); SAXReader saxReader = new SAXReader(); /** * 先读入整份XML文档 */ Document document = saxReader.read(new FileInputStream(new File(file))); Element root = document.getRootElement(); List list = root.elements(); for(int i=0;i<list.size();i++){ Element ele = (Element) list.get(i); map.put(ele.getName(), ele.getText()); } return map; } public void updateDocument() { // TODO Auto-generated method stub } }
package com.glj.util; import java.io.File; import java.util.Map; import org.apache.log4j.Logger; /** * 系统配置类 * @author Jiomer * */ public class ConfigureUtil { private static Logger logger = Logger.getLogger(ConfigureUtil.class); public static ConfigureUtil instance = null; @SuppressWarnings("rawtypes") private Map configureMap = null; public ConfigureUtil() { String file = ClassLoader.getSystemResource("").getPath()+"cfg.xml"; System.out.println(file); logger.info("获取了cfg.xml路径:"+file); try { configureMap = new XmlSupportImpl().readDocument(file); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public ConfigureUtil(String isHiber) { String file = this.getClass().getResource("/").getPath()+"cfghiber.xml"; try { configureMap = new XmlSupportImpl().readDocument(file); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getValue(String key) { return (String) configureMap.get(key); } public static ConfigureUtil getInstance(){ if(null== instance ){ synchronized(ConfigureUtil.class){ if(null== instance){ instance = new ConfigureUtil (); } } } return instance; } public static void main(String[] args) { // System.out.println(new ConfigureUtil().getValue("driverURL")); } }
end...