ConfigUtil(配置文件加载操作)

 

纯代码

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

 

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

内容类似于:

Xml代码   收藏代码
  1. #oracle version database settings  
  2. #jdbc.driver=oracle.jdbc.driver.OracleDriver  
  3. #jdbcjdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:space  
  4. #jdbc.username=space  
  5. #jdbc.password=space  
  6. #hibernate.dialect=org.hibernate.dialect.Oracle10gDialect  
  7. ###æ¬åºç¨æ°æ®åº########################################################################  
  8. jdbc.driver=com.mysql.jdbc.Driver  
  9. jdbcjdbc.url=jdbc:mysql://127.0.0.1:3306/patientfriends?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true  
  10. jdbc.username=root  
  11. jdbc.password=root  
  12. hibernate.dialect=org.hibernate.dialect.MySQLDialect  
  13. #hibernate settings  
  14. hibernate.show_sql=true  
  15. hibernate.format_sql=false  
  16. #dbcp settings  
  17. dbcp.maxIdle=5  
  18. dbcp.maxActive=40  

 

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

 

第二步:编写 配置文件加载操作类 ConfigUtil

Java代码   收藏代码
  1. public class ConfigUtil {  
  2.       
  3.     private static PropertiesConfiguration  config=null;  
  4.     static{  
  5.         try{  
  6.             //config = new PropertiesConfiguration("app.properties");  
  7.             //config.setEncoding("UTF-8");  
  8.             //config.setHeader(header)  
  9.             config = new PropertiesConfiguration();  
  10.             config.setEncoding("UTF-8");  
  11.             config.load("application.properties");  
  12.               
  13.         }catch(Exception ex){  
  14.         }  
  15.     }  
  16.       
  17.       
  18.     public static int getIntValue(String key){  
  19.         int reInt = 1;  
  20.         try{  
  21.             //PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");  
  22.             reInt = config.getInt(key);  
  23.         }catch(Exception ex){  
  24.             ex.fillInStackTrace();  
  25.             reInt = 0;  
  26.         }  
  27.         return reInt;  
  28.     }     
  29.       
  30.     public static Long getLongValue(String key) {  
  31.         Long reLong = 1l;  
  32.         try{  
  33.             //PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");  
  34.             reLong = config.getLong(key);  
  35.         }catch(Exception ex){  
  36.             ex.fillInStackTrace();  
  37.             reLong = 0l;  
  38.         }  
  39.         return reLong;  
  40.     }  
  41.       
  42.       
  43.       
  44.     public static double getDoubleValue(String key){  
  45.         double reDouble = 1.0;  
  46.         try{  
  47.             //PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");  
  48.             reDouble = config.getDouble(key);  
  49.         }catch(Exception ex){  
  50.             ex.fillInStackTrace();  
  51.             reDouble =1.0;  
  52.         }  
  53.         return reDouble;  
  54.     }  
  55.       
  56.     public static String getStringValue(String key){  
  57.         String str = "";  
  58.         try{  
  59.             //PropertiesConfiguration  config = new PropertiesConfiguration("conf.properties");  
  60.             str = config.getString(key);  
  61.         }catch(Exception ex){  
  62.             ex.fillInStackTrace();  
  63.             str = "";  
  64.         }  
  65.         return str;  
  66.     }  
  67.       
  68.     public static Boolean getBooleanValue(String key) {  
  69.         Boolean flag = false;  
  70.         try{  
  71.             flag = config.getBoolean(key);  
  72.         }catch(Exception ex){  
  73.             ex.fillInStackTrace();  
  74.         }  
  75.         return flag;  
  76.     }  
  77.       
  78.     public static void save(String key,Object o){  
  79.         config.setProperty(key, o);  
  80.         try{  
  81.             //config.save("score.properties");  
  82.             config.save("application.properties");  
  83.             //config = new PropertiesConfiguration("app.properties");  
  84.             config = new PropertiesConfiguration();  
  85.             config.setEncoding("UTF-8");  
  86.             config.load("application.properties");  
  87.         }catch(Exception ex){  
  88.             ex.printStackTrace();  
  89.         }  
  90.     }  
  91.       
  92.     public static void main(String[] args){  
  93.         System.out.println("ewew");  
  94.         System.out.println(getIntValue("vip.money"));  
  95.         //ConfigUtil.save("rmb.money", 20);  
  96.         //ConfigUtil.save("month.live", "123334");  
  97.         System.out.println(ConfigUtil.getStringValue("month.live1"));  
  98.         //System.out.println(getStringValue("month.live"));  
  99.     }  
  100.       
  101.     // 读取配置文件  
  102.     private static ResourceBundle cache = null;  
  103.   
  104.     static {  
  105.         try {  
  106.             cache = ResourceBundle.getBundle("application");  
  107.         } catch (RuntimeException e) {  
  108.             e.printStackTrace();  
  109.         }  
  110.     }  
  111.   
  112.     /** 
  113.      * 功能描述:获取配置文件参数值 
  114.      *  
  115.      * @param str(参数KEY值) 
  116.      * @return 
  117.      */  
  118.     public static String getValue(String str) {  
  119.         String s = cache.getString(str);  
  120.         try {  
  121.             s = new String(s.getBytes("ISO8859-1"), "utf-8");  
  122.         } catch (UnsupportedEncodingException e) {  
  123.             e.printStackTrace();  
  124.         }  
  125.         return s;  
  126.     }  
  127.   
  128.     /** 
  129.      * 功能描述:获取指定配置文件参数的值 
  130.      *  
  131.      * @param strPropertiesFile(配置文件名称) 
  132.      * @param strItem(参数名称) 
  133.      * @return 
  134.      */  
  135.     public String getPropertiesValue(String strPropertiesFile, String strItem) {  
  136.         String strItemValue = "";  
  137.         ResourceBundle resources1 = null;  
  138.         try {  
  139.             resources1 = ResourceBundle.getBundle(strPropertiesFile);  
  140.             strItemValue = resources1.getString(strItem);  
  141.         } catch (MissingResourceException e) {  
  142.             System.out.println("ConfigInfo.getPropertiesValue error:"  
  143.                     + e.getMessage());  
  144.         }  
  145.         return strItemValue;  
  146.     }  
  147.       
  148.       
  149.       
  150. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值