ConfigUtil(配置文件加载操作)

29 篇文章 0 订阅

纯代码

第一步:配置好配置文件:

 

配置文件置于 框架的 src 下,命名为 application.properties

内容类似于:

#oracle version database settings
#jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:space
#jdbc.username=space
#jdbc.password=space
#hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
###本应用数据库########################################################################
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/patientfriends?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
jdbc.username=root
jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
#hibernate settings
hibernate.show_sql=true
hibernate.format_sql=false
#dbcp settings
dbcp.maxIdle=5
dbcp.maxActive=40

里面存放框架中所有应该提取出的参数信息,比如:数据库连接信息,定时器时间戳,Email相关参数信息,图片宽高参数,URl,用户名密码等等。

public class ConfigUtil {
	
	private static PropertiesConfiguration  config=null;
	static{
		try{
			//config = new PropertiesConfiguration("app.properties");
			//config.setEncoding("UTF-8");
			//config.setHeader(header)
			config = new PropertiesConfiguration();
			config.setEncoding("UTF-8");
			config.load("application.properties");
			
		}catch(Exception ex){
		}
	}
	
	
	public static int getIntValue(String key){
		int reInt = 1;
		try{
			//PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");
			reInt = config.getInt(key);
		}catch(Exception ex){
			ex.fillInStackTrace();
			reInt = 0;
		}
		return reInt;
	}	
	
	public static Long getLongValue(String key) {
		Long reLong = 1l;
		try{
			//PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");
			reLong = config.getLong(key);
		}catch(Exception ex){
			ex.fillInStackTrace();
			reLong = 0l;
		}
		return reLong;
	}
	
	
	
	public static double getDoubleValue(String key){
		double reDouble = 1.0;
		try{
			//PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");
			reDouble = config.getDouble(key);
		}catch(Exception ex){
			ex.fillInStackTrace();
			reDouble =1.0;
		}
		return reDouble;
	}
	
	public static String getStringValue(String key){
		String str = "";
		try{
			//PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");
			str = config.getString(key);
		}catch(Exception ex){
			ex.fillInStackTrace();
			str = "";
		}
		return str;
	}
	
	public static Boolean getBooleanValue(String key) {
		Boolean flag = false;
		try{
			flag = config.getBoolean(key);
		}catch(Exception ex){
			ex.fillInStackTrace();
		}
		return flag;
	}
	
	public static void save(String key,Object o){
		config.setProperty(key, o);
		try{
			//config.save("score.properties");
			config.save("application.properties");
			//config = new PropertiesConfiguration("app.properties");
			config = new PropertiesConfiguration();
			config.setEncoding("UTF-8");
			config.load("application.properties");
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public static void main(String[] args){
		System.out.println("ewew");
		System.out.println(getIntValue("vip.money"));
		//ConfigUtil.save("rmb.money", 20);
		//ConfigUtil.save("month.live", "123334");
		System.out.println(ConfigUtil.getStringValue("month.live1"));
		//System.out.println(getStringValue("month.live"));
	}
	
	// 读取配置文件
	private static ResourceBundle cache = null;

	static {
		try {
			cache = ResourceBundle.getBundle("application");
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 功能描述:获取配置文件参数值
	 * 
	 * @param str(参数KEY值)
	 * @return
	 */
	public static String getValue(String str) {
		String s = cache.getString(str);
		try {
			s = new String(s.getBytes("ISO8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return s;
	}

	/**
	 * 功能描述:获取指定配置文件参数的值
	 * 
	 * @param strPropertiesFile(配置文件名称)
	 * @param strItem(参数名称)
	 * @return
	 */
	public String getPropertiesValue(String strPropertiesFile, String strItem) {
		String strItemValue = "";
		ResourceBundle resources1 = null;
		try {
			resources1 = ResourceBundle.getBundle(strPropertiesFile);
			strItemValue = resources1.getString(strItem);
		} catch (MissingResourceException e) {
			System.out.println("ConfigInfo.getPropertiesValue error:"
					+ e.getMessage());
		}
		return strItemValue;
	}
	
	
	
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值