当前位置 博文首页 > 文章内容

    properties文档读取工具类

    作者: 栏目:未分类 时间:2020-09-23 17:59:49

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    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