解析property文件
来源:互联网 发布:迅雷看看有mac版吗 编辑:程序博客网 时间:2024/06/14 10:41
package com.jg.util;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.util.Enumeration;import java.util.Map;import java.util.Properties;import java.util.TreeMap;/** * This is the single entry point for accessing configuration properties. * */public class SystemConfig{ private static Properties mConfig; static { mConfig = new Properties(); try{ //绝对路径 mConfig.load(new BufferedInputStream(new FileInputStream(System.getProperty("user.dir") + "/system-redis.properties"))); }catch(Exception exp1){ try{ //相对路径 mConfig.load(SystemConfig.class.getClassLoader().getResourceAsStream("system-redis.properties")); }catch(Exception exp2){ exp2.printStackTrace(); Thread.currentThread().stop(); } } } // no, you may not instantiate this class :p private SystemConfig() { } /** * Retrieve a property value * * @param key * Name of the property * @return String Value of property requested, null if not found */ public static String getProperty(String key) { return mConfig.getProperty(key); } public static String getProperty(String key, String defaultValue) { String value = SystemConfig.getProperty(key); if (value == null) { return defaultValue; } return value; } /** * Retrieve a property as a boolean ... defaults to false if not present. */ public static boolean getBooleanProperty(String name) { return getBooleanProperty(name, false); } /** * Retrieve a property as a boolean ... with specified default if not present. */ public static boolean getBooleanProperty(String name, boolean defaultValue) { // get the value first, then convert String value = SystemConfig.getProperty(name); if (value == null) { return defaultValue; } return (new Boolean(value)).booleanValue(); } /** * Retrieve a property as a int ... defaults to 0 if not present. * * @param name * @return */ public static int getIntProperty(String name) { return getIntProperty(name, 0); } /** * 返回指定默认值 * * @param name * @param defaultValue * @return */ public static int getIntProperty(String name, int defaultValue) { // get the value first, then convert String value = SystemConfig.getProperty(name); if (value == null) { return defaultValue; } try { return Integer.parseInt(value); } catch (NumberFormatException e) { return defaultValue; } } /** * 返回指定默认值的int数组 * * @param name * @param defaultValue * @author wondtech liangming * @date 2008-07-25 * @return int[] */ public static int[] getIntPropertyArray(String name, int[] defaultValue) { // get the value first, then convert String value = SystemConfig.getProperty(name); if (value == null) { return defaultValue; } try { String[] propertyArray = value.split(",");// 将字符用逗开分离 int[] result = new int[propertyArray.length]; for (int i = 0; i < propertyArray.length; i++) {// result[i] = Integer.parseInt(propertyArray[i]); } return result; } catch (NumberFormatException e) { return defaultValue; } } /** * 返回指定默认值的boolean数组 * * @param name * @param defaultValue * @author wondtech liangming * @date 2008-07-25 * @return boolean[] */ public static boolean[] getBooleanPropertyArray(String name, boolean[] defaultValue) { // get the value first, then convert String value = SystemConfig.getProperty(name); if (value == null) { return defaultValue; } try { String[] propertyArray = value.split(",");// 将字符用逗开分离 boolean[] result = new boolean[propertyArray.length]; for (int i = 0; i < propertyArray.length; i++) {// result[i] = (new Boolean(propertyArray[i])).booleanValue(); } return result; } catch (NumberFormatException e) { return defaultValue; } } /** * 返回指定默认值的str数组 * * @param name * @param defaultValue * @author wondtech liangming * @date 2008-07-25 * @return String[] */ public static String[] getPropertyArray(String name, String[] defaultValue) { // get the value first, then convert String value = SystemConfig.getProperty(name); if (value == null) { return defaultValue; } try { String[] propertyArray = value.split(",");// 将字符用逗开分离 return propertyArray; } catch (NumberFormatException e) { return defaultValue; } } /** * 返回指定默认值的str数组 * * @param name * @param defaultValue * @author wondtech liangming * @date 2008-07-25 * @return String[] */ public static String[] getPropertyArray(String name) { // get the value first, then convert String value = SystemConfig.getProperty(name); if (value == null) { return null; } try { String[] propertyArray = value.split(",");// 将字符用逗开分离 return propertyArray; } catch (NumberFormatException e) { return null; } } /** * Retrieve all property keys * * @return Enumeration A list of all keys */ public static Enumeration keys() { return mConfig.keys(); } public static Map<String,Object> getPropertyMap(String name) { String[] maps = getPropertyArray(name); Map<String,Object> map = new TreeMap<String,Object>(); try { for (String str : maps) { String[] array = str.split(":"); if (array.length > 1) { map.put(array[0], array[1]); } } } catch (Exception e) { e.printStackTrace(); } return map; }}
0 0
- Java解析Property文件
- 解析property文件
- @property 解析
- java web开发中property文件java程序解析
- Hibernate的Property解析
- Hibernate的Property解析
- Android System Property 解析
- Hibernate的Property解析
- 解析@property 和 @synthesize
- 1114. Family Property 解析
- property-placeholder源码解析
- java 读取property文件
- java 读取property文件
- 读取 .property文件
- java读取property文件
- java读写property文件
- Property List 文件
- spring加载property文件
- mac android studio 配置 gradle
- 算法笔记_070-BellmanFord算法简单介绍(Java)
- uboot下init_sequence_f函数之jump_to_copy
- 程序猿的35岁悖论:这是一碗“青春饭”,迟早要转行?
- synchronized同步函数
- 解析property文件
- FishC笔记—23,24 讲 递归:这帮小兔崽子,汉诺塔
- Android——动态广播实例
- hbase命令
- 死锁
- Vue.js学习系列(十九)--常用指令(二)
- 【知识点】大数分解与素数判定 --- 【Miller-rabin算法】【pollard-rho算法】
- VM虚拟机下安装Centos7.0图文教程
- 在浏览器中输入www.baidu.com后执行的全部过程