zoukankan      html  css  js  c++  java
  • 获取配置文件工具类


    package main.java.sinosoft.utils; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Properties; import org.apache.http.impl.io.SocketOutputBuffer; import org.apache.log4j.Logger; /** * 获取配置文件(读取和jar包同级目录的config.properties的配置文件) * * @author yqzhilan * */ public class PropertyUtils { protected static Logger logger = Logger.getLogger(PropertyUtils.class); //保存系统文件。 private static String sysFile = "config.properties"; private static Properties Sysproperties ; static { Sysproperties = PropertyUtils.getProperties(sysFile); } /** * 获取指定路径下的配置文件信息 * * @param configPath * @return */ public static Properties getProperties(String configPath) { Properties pros = new Properties(); try { ClassLoader cl = ClassLoader.getSystemClassLoader(); ClassLoader.getSystemClassLoader().getResource(sysFile); pros.load(new InputStreamReader(cl.getResourceAsStream(sysFile), "UTF-8")); } catch (IOException e) { e.printStackTrace(); } return pros; } public static String getSysConfigSet(String key){ return Sysproperties.getProperty(key); } }

      获取类路径下的配置文件 

    public class PropertyUitls {
        protected static Logger logger = Logger.getLogger(PropertyUitls.class);
         //保存系统文件。
         private static String sysFile = "config.properties";
         private static Properties Sysproperties ;
         static {
             Sysproperties = PropertyUitls.getProperties(sysFile);
         }
        
        /**
         * 获取指定路径下的配置文件信息
         * 
         * @param configPath
         * @return
         */
        public static Properties getProperties(String configPath) {
            Properties pros = new Properties();
            try {
                ClassLoader cl = PropertyUitls.class.getClassLoader();
                pros.load(new InputStreamReader(cl.getResourceAsStream(configPath), "UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return pros;
        }
        
        public static String getSysConfigSet(String key){
            return Sysproperties.getProperty(key);
        }
        
     
    }
  • 相关阅读:
    【TECH】CAS php客户端配置
    【Tech】CAS多机部署Server和Java Client端
    【Tech】CAS RESTful API使用笔记
    【读书笔记】《Java Web整合开发实践》第3章 JSP
    MVC
    Echarts
    AutoMapper
    EntityFramework 二
    EntityFramework 一
    C# 基础复习 四 多线程
  • 原文地址:https://www.cnblogs.com/xiaofuzi123456/p/11249551.html
Copyright © 2011-2022 走看看