zoukankan      html  css  js  c++  java
  • 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(); } }
  • 相关阅读:
    2018年强化学习领域十篇重要论文(附源码)[转]
    【转载】 再励学习面试真题 (强化学习面试真题)
    枸杞常泡水喝好吗?
    亲历亚马逊、华为机器学习面试,原来考官想听到这些回答[转]
    阿里面试 深度学习[转]
    强化学习(四)用蒙特卡罗法(MC)求解
    初识kaggle,以及记录 kaggle的使用
    强化学习(三)用动态规划(DP)求解
    CORTEX-M3中断的现场保护问题
    地球是圆的怎么还分东西方
  • 原文地址:https://www.cnblogs.com/hwj2wj/p/2856176.html
Copyright © 2011-2022 走看看