Java PropertiesLoader 工具类 开发者使用

具类
  1. /** 
  2.  * Copyright (c) 2005-2011 springside.org.cn 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  *  
  6.  * $Id: PropertiesLoader.java 1690 2012-02-22 13:42:00Z calvinxiu $ 
  7.  */  
  8. package com.zltd.sto.common.utils;  
  9.   
  10. import java.io.IOException;  
  11. import java.io.InputStream;  
  12. import java.util.NoSuchElementException;  
  13. import java.util.Properties;  
  14.   
  15. import org.apache.commons.io.IOUtils;  
  16. import org.slf4j.Logger;  
  17. import org.slf4j.LoggerFactory;  
  18. import org.springframework.core.io.DefaultResourceLoader;  
  19. import org.springframework.core.io.Resource;  
  20. import org.springframework.core.io.ResourceLoader;  
  21.   
  22. /** 
  23.  * Properties文件载入工具类. 可载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先. 
  24.  * @author calvin 
  25.  * @version 2013-01-15 
  26.  */  
  27. public class PropertiesLoader {  
  28.   
  29.     private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);  
  30.   
  31.     private static ResourceLoader resourceLoader = new DefaultResourceLoader();  
  32.   
  33.     private final Properties properties;  
  34.   
  35.     public PropertiesLoader(String... resourcesPaths) {  
  36.         properties = loadProperties(resourcesPaths);  
  37.     }  
  38.   
  39.     public Properties getProperties() {  
  40.         return properties;  
  41.     }  
  42.   
  43.     /** 
  44.      * 取出Property,但以System的Property优先. 
  45.      */  
  46.     private String getValue(String key) {  
  47.         String systemProperty = System.getProperty(key);  
  48.         if (systemProperty != null) {  
  49.             return systemProperty;  
  50.         }  
  51.         return properties.getProperty(key);  
  52.     }  
  53.   
  54.     /** 
  55.      * 取出String类型的Property,但以System的Property优先,如果都為Null则抛出异常. 
  56.      */  
  57.     public String getProperty(String key) {  
  58.         String value = getValue(key);  
  59.         if (value == null) {  
  60.             throw new NoSuchElementException();  
  61.         }  
  62.         return value;  
  63.     }  
  64.   
  65.     /** 
  66.      * 取出String类型的Property,但以System的Property优先.如果都為Null則返回Default值. 
  67.      */  
  68.     public String getProperty(String key, String defaultValue) {  
  69.         String value = getValue(key);  
  70.         return value != null ? value : defaultValue;  
  71.     }  
  72.   
  73.     /** 
  74.      * 取出Integer类型的Property,但以System的Property优先.如果都為Null或内容错误则抛出异常. 
  75.      */  
  76.     public Integer getInteger(String key) {  
  77.         String value = getValue(key);  
  78.         if (value == null) {  
  79.             throw new NoSuchElementException();  
  80.         }  
  81.         return Integer.valueOf(value);  
  82.     }  
  83.   
  84.     /** 
  85.      * 取出Integer类型的Property,但以System的Property优先.如果都為Null則返回Default值,如果内容错误则抛出异常 
  86.      */  
  87.     public Integer getInteger(String key, Integer defaultValue) {  
  88.         String value = getValue(key);  
  89.         return value != null ? Integer.valueOf(value) : defaultValue;  
  90.     }  
  91.   
  92.     /** 
  93.      * 取出Double类型的Property,但以System的Property优先.如果都為Null或内容错误则抛出异常. 
  94.      */  
  95.     public Double getDouble(String key) {  
  96.         String value = getValue(key);  
  97.         if (value == null) {  
  98.             throw new NoSuchElementException();  
  99.         }  
  100.         return Double.valueOf(value);  
  101.     }  
  102.   
  103.     /** 
  104.      * 取出Double类型的Property,但以System的Property优先.如果都為Null則返回Default值,如果内容错误则抛出异常 
  105.      */  
  106.     public Double getDouble(String key, Integer defaultValue) {  
  107.         String value = getValue(key);  
  108.         return value != null ? Double.valueOf(value) : defaultValue;  
  109.     }  
  110.   
  111.     /** 
  112.      * 取出Boolean类型的Property,但以System的Property优先.如果都為Null抛出异常,如果内容不是true/false则返回false. 
  113.      */  
  114.     public Boolean getBoolean(String key) {  
  115.         String value = getValue(key);  
  116.         if (value == null) {  
  117.             throw new NoSuchElementException();  
  118.         }  
  119.         return Boolean.valueOf(value);  
  120.     }  
  121.   
  122.     /** 
  123.      * 取出Boolean类型的Property,但以System的Property优先.如果都為Null則返回Default值,如果内容不为true/false则返回false. 
  124.      */  
  125.     public Boolean getBoolean(String key, boolean defaultValue) {  
  126.         String value = getValue(key);  
  127.         return value != null ? Boolean.valueOf(value) : defaultValue;  
  128.     }  
  129.   
  130.     /** 
  131.      * 载入多个文件, 文件路径使用Spring Resource格式. 
  132.      */  
  133.     private Properties loadProperties(String... resourcesPaths) {  
  134.         Properties props = new Properties();  
  135.   
  136.         for (String location : resourcesPaths) {  
  137.   
  138. //          logger.debug("Loading properties file from:" + location);  
  139.   
  140.             InputStream is = null;  
  141.             try {  
  142.                 Resource resource = resourceLoader.getResource(location);  
  143.                 is = resource.getInputStream();  
  144.                 props.load(is);  
  145.             } catch (IOException ex) {  
  146.                 logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());  
  147.             } finally {  
  148.                 IOUtils.closeQuietly(is);  
  149.             }  
  150.         }  
  151.         return props;  
  152.     }  


原文出自:点击打开链接http://blog.csdn.net/hurryjiang/article/details/8778865

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值