Java-Properties文件读取工具类

  1 import org.apache.commons.configuration.ConfigurationException;
  2 import org.apache.commons.configuration.PropertiesConfiguration;
  3 
  4 import com.alibaba.fastjson.JSON;
  5 import com.alibaba.fastjson.JSONObject;
  6 
  7 /**
  8  * @ClassName: ReadPropertiesUtil
  9  */
 10 public class ReadPropertiesUtil {
 11     // 声明配置文件
 12     private static String[] confProps = { "config.properties" };
 13     private static PropertiesConfiguration conf = null;
 14 
 15     /**
 16      * 获取所有配置对象
 17      */
 18     public static PropertiesConfiguration getConf() {
 19         conf = new PropertiesConfiguration();// 初始化对象
 20         /* 把所有props配置文件一次加载,共后续使用 */
 21         for (int i = 0; i < confProps.length; i++) {
 22             try {
 23                 conf.load(confProps[i]);
 24             } catch (ConfigurationException e) {
 25                 conf = null;
 26             }
 27         }
 28         return conf;
 29     }
 30 
 31     /*
 32      * PropsUtil getInt()
 33      */
 34     public static Integer getInt(String key) {
 35         /** 声明返回值 **/
 36         Integer value = null;
 37         try {
 38             /* 获取value值 */
 39             conf = getConf();
 40             value = conf.getInt(key);
 41         } catch (Exception e) {
 42         }
 43         return value;
 44     }
 45 
 46     /*
 47      * PropsUtil getString()
 48      */
 49     public static String getString(String key) {
 50         /** 声明返回值 **/
 51         String value = null;
 52         try {
 53             /* 获取value值 */
 54             conf = getConf();
 55             value = conf.getString(key);
 56         } catch (Exception e) {
 57         }
 58         return value;
 59     }
 60 
 61     /*
 62      * PropsUtil getLong()
 63      */
 64     public static Long getLong(String key) {
 65         /** 声明返回值 **/
 66         Long value = null;
 67         try {
 68             /* 获取value值 */
 69             conf = getConf();
 70             value = conf.getLong(key);
 71         } catch (Exception e) {
 72         }
 73         return value;
 74     }
 75 
 76     /*
 77      * PropsUtil getBoolean()
 78      */
 79     public static boolean getBoolean(String key) {
 80         /** 声明返回值 **/
 81         boolean value = true;
 82         try {
 83             /* 获取value值 */
 84             conf = getConf();
 85             value = conf.getBoolean(key);
 86         } catch (Exception e) {
 87             value = false;
 88         }
 89         return value;
 90     }
 91 
 92     /*
 93      * PropsUtil getList()
 94      */
 95     public static String[] getStringArray(String key) {
 96         /** 声明返回值 **/
 97         String[] value = null;
 98         try {
 99             /* 获取value值 */
100             conf = getConf();
101             value = conf.getStringArray(key);
102         } catch (Exception e) {
103         }
104         return value;
105     }
106 
107     /*
108      * PropsUtil getJsonHeaders()
109      */
110     public static JSONObject getJsonHeaders(String key) {
111         JSONObject json = null;// 声明返回值
112         try {
113             /** 解析*.Properties文件 **/
114             conf = getConf();
115             String value = conf.getString(key).replace("=", ",");
116             json = JSON.parseObject(value);
117         } catch (Exception e) {
118         }
119         return json;
120     }
121 
122     public static void main(String[] args) throws Exception {
123 
124         System.out.println(getString("dbcp.url"));
125 
126         // String[] tmp = getStringArray("Email_to");
127         // for(String to:tmp){
128         // System.out.println(to);
129         // }
130 
131         // JSONObject json = getJsonHeaders("httpHead");
132         // for(Entry<String, Object> entry:json.entrySet()){
133         // System.out.println(entry.getKey() + "-----" + entry.getValue().toString());
134         // }
135 
136         // System.out.println(getInt("dbcp.url"));
137 
138         // System.out.println(getInt("redis.minIdle"));
139         // String[] tmp = getStringArray("redis.redisSlaveIp");
140         // for(int i=0;i<tmp.length;i++){
141         // System.out.println(tmp[i].split(":")[0]);
142         // System.out.println(tmp[i].split(":")[1]);
143         // }
144     }
145 }
 1 //相关依赖
 2         <!-- commons-configuration -->
 3         <dependency>
 4             <groupId>commons-configuration</groupId>
 5             <artifactId>commons-configuration</artifactId>
 6             <version>1.9</version>
 7         </dependency>
 8         <!-- fastjson json -->
 9         <dependency>
10             <groupId>com.alibaba</groupId>
11             <artifactId>fastjson</artifactId>
12             <version>1.2.7</version>
13         </dependency>

 Properties文件载入工具类:

  依赖spring等。

  

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

猜你喜欢

转载自www.cnblogs.com/wang1001/p/9768071.html