zoukankan      html  css  js  c++  java
  • properties文档读取工具类

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    /**
     * 获取properties文档
     */
    public class GetProperties {
        /**
         * 日志记录
         */
        protected final static Log log = LogFactory.getLog(GetProperties.class);
        
        /**读取项目中的properties文件
         */
        public static Map<String,String> readProperties(String fileName){
            Map<String,String> result = new HashMap<String,String>();
            InputStream inputStream = null;
            try {
                // 加载配置文件
                inputStream = GetProperties.class.getResourceAsStream("./../../michael/properties/"+fileName);
                Properties props = new Properties();
                props.load(inputStream);
                for(Object key : props.keySet()){
                    result.put((String)key, props.getProperty((String)key));
                }
                return result;
            } catch (Exception e) {
                System.err.println("..............读取" + fileName + "错误");
                log.error(e.getMessage());
            } finally {
                try {
                    if(inputStream!=null)
                        inputStream.close();
                } catch (IOException e) {
                }
            }
            return null;
        }
        public static Map<String,String> readProperties(File file){
            Map<String,String> result = new HashMap<String,String>();
            InputStream inputStream = null;
            try {
                // 加载配置文件
                inputStream = new FileInputStream(file);
                Properties props = new Properties();
                props.load(inputStream);
                for(Object key : props.keySet()){
                    result.put((String)key, props.getProperty((String)key));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
            return result;
        }
        /**读取tomcat中的properties文件
         */
        public static Map<String,String> readTomcatBinProperties(String fileName){
            Map<String,String> result = new HashMap<String,String>();
            InputStream inputStream = null;
            try {
                // 加载配置文件
                inputStream = new FileInputStream(new File(".").getPath() + File.separator + fileName);
                Properties props = new Properties();
                props.load(inputStream);
                for(Object key : props.keySet()){
                    result.put((String)key, props.getProperty((String)key));
                }
                return result;
            } catch (Exception e) {
                System.err.println("..............读取" + fileName + "错误");
                log.error(e.getMessage());
            } finally {
                try {
                    if(inputStream!=null)
                        inputStream.close();
                } catch (IOException e) {
                }
            }
            return null;
        }
    
    }
    View Code
  • 相关阅读:
    [daily][netcat] 在UNIX socket上使用netcat
    [emacs] emacs设置python code的indent
    [dev][python] 从python2进阶到python3你都需要了解什么
    [strongswan][autoconf][automake][cento] 在CentOS上编译strongswan git源码时遇到的autoconf问题
    [strongswan] strongswan是如何实现与xfrm之间的trap机制的
    对不可描述的软件安装sfbo插件
    [daily] 如何用emacs+xcscope阅读内核源码
    [daily] cscope
    [dev][ipsec] 什么是xfrm
    [dev][ipsec] netlink是什么
  • 原文地址:https://www.cnblogs.com/bl123/p/13719988.html
Copyright © 2011-2022 走看看