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(); } }
  • 相关阅读:
    base64和Blob的相互转换
    限制文件上传的大小和尺寸
    git将本地项目提交到github
    vue-cli3创建项目时报错
    运行项目是node-sass报错的解决方法
    classList的使用
    将数组扁平化并去除其中重复数据,最终得到一个升序且不重复的数组
    移动端的图片放大
    js获取url中的参数
    HTML5-canvas
  • 原文地址:https://www.cnblogs.com/hwj2wj/p/2856176.html
Copyright © 2011-2022 走看看