您的位置:首页 > Web前端 > JavaScript

Code片段 : .properties属性文件操作工具类 & JSON工具类

2016-07-19 23:41 507 查看
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!“贵专” — 泥瓦匠

一、java.util.Properties API & 案例

java.util.Properties 是一个属性集合。常见的api有如下:
load(InputStream inStream)  从输入流中读取属性
getProperty(String key)  根据key,获取属性值
getOrDefault(Object key, V defaultValue) 根据key对象,获取属性值需要强转

首先在resources目录下增加/main/resources/fast.properties:
/**
* .properties属性文件操作工具类
*
* Created by bysocket on 16/7/19.
*/
public class PropertyUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(PropertyUtil.class);

/** .properties属性文件名后缀 */
public static final String PROPERTY_FILE_SUFFIX = ".properties";

/**
* 根据属性文件名,获取属性
*
* @param propsFileName
* @return
*/
public static Properties getProperties(String propsFileName) {
if (StringUtils.isEmpty(propsFileName))
throw new IllegalArgumentException();

Properties properties = new Properties();
InputStream inputStream = null;

try {

try {
/** 加入文件名后缀 */
if (propsFileName.lastIndexOf(PROPERTY_FILE_SUFFIX) == -1) {
propsFileName += PROPERTY_FILE_SUFFIX;
}

inputStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(propsFileName);
if (null != inputStream)
properties.load(inputStream);
} finally {
if ( null != inputStream) {
inputStream.close();
}
}

} catch (IOException e) {
LOGGER.error("加载属性文件出错!",e);
throw new RuntimeException(e);
}

return properties;
}

/**
* 根据key,获取属性值
*
* @param properties
* @param key
* @return
*/
public static String getString(Properties properties, String key){
return properties.getProperty(key);
}

/**
* 根据key,获取属性值
*
* @param properties
* @param key
* @param defaultValue
* @return
*/
public static String getStringOrDefault(Properties properties, String key, String defaultValue){
return properties.getProperty(key,defaultValue);
}

/**
* 根据key,获取属性值
*
* @param properties
* @param key
* @param defaultValue
* @param <V>
* @return
*/
public static <V> V getOrDefault(Properties properties, String key, V defaultValue){
return (V) properties.getOrDefault(key,defaultValue);
}
}

UT如下:
/**
* {@link PropertyUtil} 测试用例
* <p/>
* Created by bysocket on 16/7/19.
*/
public class PropertyUtilTest {

@Test
public void testGetProperties() {
Properties properties = PropertyUtil.getProperties("fast");
String fastFrameworkName = properties.getProperty("fast.framework.name");
String authorName        = properties.getProperty("fast.framework.author");
Object age               = properties.getOrDefault("fast.framework.age",10);
Object defaultVal        = properties.getOrDefault("fast.framework.null",10);
System.out.println(fastFrameworkName);
System.out.println(authorName);
System.out.println(age.toString());
System.out.println(defaultVal.toString());
}

@Test
public void testGetString() {
Properties properties = PropertyUtil.getProperties("fast");
String fastFrameworkName = PropertyUtil.getString(properties,"fast.framework.name");
String authorName        = PropertyUtil.getString(properties,"fast.framework.author");
System.out.println(fastFrameworkName);
System.out.println(authorName);
}

@Test
public void testGetOrDefault() {
Properties properties = PropertyUtil.getProperties("fast");
Object age               = PropertyUtil.getOrDefault(properties,"fast.framework.age",10);
Object defaultVal        = PropertyUtil.getOrDefault(properties,"fast.framework.null",10);
System.out.println(age.toString());
System.out.println(defaultVal.toString());
}
}

Run Console:
1
10
fast
bysocket
1
10
fast
bysocket
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: