zoukankan      html  css  js  c++  java
  • java读取properties配置文件

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    /**
     * Properties类的使用,这样使用这个类的可以方便的取到配置文件里的东西
     * 我们在其他类中直接调用以下方式便能读取到配置
     * SystemConfig.getProperty("name");
     * @author leejuen
     *
     */
    public class SystemConfig {
    	private static Properties systemConfig;
    	private static String configPath;
    	private SystemConfig() {}
    	static
    	{
    		systemConfig = new Properties();
    		configPath = "./sys-config.properties";    //设置配置文件路径
    		try {
    			/**
    			 * 在使用Class.getResourceAsStream 时, 资源路径有两种方式,
    			 * 一种以 / 开头,则这样的路径是指定绝对路径, 如果不以 / 开头,
    			 * 则路径是相对与这个class所在的包的.在使用ClassLoader.getResourceAsStream时,
    			 * 路径直接使用相对于classpath的绝对路径。
    			 */
    			Class classConfig = Class.forName("com.szkingdom.leejuen.SystemConfig");
    			InputStream is = classConfig.getResourceAsStream(configPath);
    			//InputStream is = new FileInputStream("sys-config.properties");
    			systemConfig.load(is);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}	
    	}
    	public static String getProperty(String key) {
    		return systemConfig.getProperty(key);
    	}
    }
    
    
    



  • 相关阅读:
    jmeter beanshell 从文件中获取随机参数
    shell 备份mysql
    Centos7 搭建wordpress
    jmeter分布式测试
    centos 环境搭建jenkins服务
    VMVare 虚拟机使用桥接模式
    Gradle 打可执行jar包
    appium 报错
    corda
    android构建过程
  • 原文地址:https://www.cnblogs.com/leejuen/p/5547475.html
Copyright © 2011-2022 走看看