获取配置文件工具类


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);
    }
    
 
}
原文地址:https://www.cnblogs.com/xiaofuzi123456/p/11249551.html