• java对配置文件(XML/properties)的操作


    1.对properties文件的操作:

    public class PropUtil {
        private static Map<String, String> map = new HashMap<String,String>();
        private PropUtil() {
            ResourceBundle prop = ResourceBundle.getBundle("config");
            Enumeration<String> em = prop.getKeys();
            while (em.hasMoreElements()) {
                String topic = (String) em.nextElement();
                map.put(topic, (String)prop.getObject(topic));
            }
        }
        public static String getValueByTagName(String tagName){
            if(map.get(tagName)==null){
                  new PropUtil();
            }
            return map.get(tagName);
        }
    }
    import com.yjdgis.database.*;
    import com.yjdgis.jcio.JCPGInput;
    import com.yjdgis.log.LogFactory;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.logging.Level;
    import org.apache.log4j.Logger;
    
    public class Configuration {
    
        private Properties props;
        private FileInputStream ips;
       // static Logger LOG = LogFactory.getInstance(Configuration.class);
    
        public Configuration() {
            props = new Properties();
        }
    
        public Configuration(String filepath) {//加载属性配置文件
            props = new Properties();
            try {
                ips = new FileInputStream(filepath);
                props.load(ips);
                ips.close();
            } catch (Exception e) {
                e.printStackTrace();
            } 
        }
    
        /**
         * 通过属性名获取属性值
         * @param key 属性名
         * @return
         */
        public String getValue(String key) {
            if (props.containsKey(key)) {
                String value = props.getProperty(key);
                return value;
            } else {
                return "";
            }
    
        }
    
        /**
         * 
         * @param key
         * @param value
         * @param isSave 
         */
        public void setValue(String filename, String key, String value, boolean isSave) {
            if (props.containsKey(key)) {
                props.setProperty(key, value);
            }
            if (isSave) {
    
                FileOutputStream out;
                try {
                    out = new FileOutputStream(filename);
                    props.store(out, "This file is a envir");
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 在指定的属性文件中获取相关属性名的属性值
         * @param fileName
         * @param key
         * @return
         */
        public String getValue(String fileName, String key) {
            try {
                String value = null;
                ips = new FileInputStream(fileName);
                props.load(ips);
                ips.close();
                if (props.containsKey(key)) {
                    value = props.getProperty(key);
                    return value;
                } else {
                    return value;
                }
            } catch (Exception e) {
                //LOG.info(fileName + ",file doesn't exist!");
                e.printStackTrace();
                return "";
            } 
        }
    
        public void clear() {
            props.clear();
        }
    }

     2、对XML的操作

    package com.kmyjd.utils;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import org.dom4j.Document;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;
    
    public class XmlUtils {
        private static String filePath;
        static{
            filePath = XmlUtils.class.getClassLoader().getResource("user.xml").getPath();
        }
        
        public static Document getDocument() throws Exception{
            SAXReader reader = new SAXReader();
            Document  document = reader.read(new File(filePath));
            return document;
        }
        
        public static void write2Xml(Document document) throws Exception,
         FileNotFoundException{ OutputFormat format
    = OutputFormat.createCompactFormat(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream(filePath)); writer.write(document); writer.close(); } }
  • 相关阅读:
    cocos2d-X json
    CSS3背景 background-size
    HTML、CSS、meta常用代码
    less相关知识点总结
    使用elementUI滚动条之横向滚动
    text-fill-color:仿苹果官网介绍效果 CSS设置文字渐变效果 文字背景图遮罩
    CSS 解析原理_你知道浏览器CSS是如何解析吗?
    CSS新特性contain,控制页面的重绘与重排
    php使用 header 头下载文件
    Vue单页面应用阻止浏览器记住密码
  • 原文地址:https://www.cnblogs.com/hwj2wj/p/2856176.html
Copyright © 2020-2023  润新知