package com.ghg.smrtframwork.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PropsUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(PropsUtil.class);
/**
* 加载属性文件
* @Title: laodProps
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param fileName
* @param @return 设定文件
* @return Properties 返回类型
* @throws
*/
public static Properties loadProps(String fileName) {
Properties props = null;
InputStream is = null;
try {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
if (is == null) {
throw new FileNotFoundException(fileName + "file is not found");
}
props = new Properties();
props.load(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
LOGGER.error("load file failure", e);
} catch (IOException e) {
e.printStackTrace();
LOGGER.error("load properties file failure", e);
} finally {
IOUtil.close(is);
}
return props;
}
/**
* 获取字符属性
* @Title: getString
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param props
* @param @param key
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String getString(Properties props, String key) {
return getString(props, key, "");
}
public static String getString(Properties props, String key, String defaultValue) {
String value = defaultValue;
if (props.containsKey(key)) {
value = props.getProperty(key, defaultValue);
}
return value;
}
/**
* 获取int类型的值
* @Title: getInt
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param props
* @param @param key
* @param @return 设定文件
* @return int 返回类型
* @throws
*/
public static int getInt(Properties props, String key) {
return getInt(props, key, 0);
}
public static int getInt(Properties props, String key, int defaultValue) {
int value = defaultValue;
if (props.containsKey(key)) {
value = CastUtil.castInt(props.getProperty(key));
}
return value;
}
/**
* 获取boolean
* @Title: getBoolean
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param props
* @param @param key
* @param @return 设定文件
* @return boolean 返回类型
* @throws
*/
public static boolean getBoolean(Properties props, String key) {
return getBoolean(props, key, false);
}
public static boolean getBoolean(Properties props, String key,boolean defaultValue) {
boolean value = defaultValue;
if (props.containsKey(key)) {
value = CastUtil.castBoolean(props.getProperty(key));
}
return value;
}
}
package com.ghg.smrtframwork.util;
import org.apache.commons.lang3.StringUtils;
public class CastUtil {
/**
* 转换为String
* @Title: castString
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param obj
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String castString(Object obj) {
return CastUtil.castString(obj, "");
}
public static String castString(Object obj, String defaultValue) {
return obj != null ? String.valueOf(obj) : defaultValue;
}
/**
* 转换为double
* @Title: castDouble
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param obj
* @param @return 设定文件
* @return double 返回类型
* @throws
*/
public static double castDouble(Object obj) {
return CastUtil.castDouble(obj, 0);
}
public static double castDouble(Object obj, double defaultValue) {
double doubleValue = defaultValue;
if (obj != null) {
String strValue = castString(obj);
if (StringUtils.isNotEmpty(strValue)) {
try {
doubleValue = Double.parseDouble(strValue);
} catch (NumberFormatException e) {
doubleValue = defaultValue;
}
}
}
return doubleValue;
}
/**
* 转换为long
* @Title: castLong
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param obj
* @param @return 设定文件
* @return long 返回类型
* @throws
*/
public static long castLong(Object obj) {
return CastUtil.castLong(obj, 0);
}
public static long castLong(Object obj, long defaultValue) {
long longValue = defaultValue;
if (obj != null) {
String strValue = castString(obj);
if (StringUtils.isNotEmpty(strValue)) {
try {
longValue = Long.parseLong(strValue);
} catch (NumberFormatException e) {
longValue = defaultValue;
}
}
}
return longValue;
}
/**
* 转换为int
* @Title: castInt
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param property
* @param @return 设定文件
* @return int 返回类型
* @throws
*/
public static int castInt(Object obj) {
return CastUtil.castInt(obj, 0);
}
public static int castInt(Object obj, int defaultValue) {
int intValue = defaultValue;
if (obj != null) {
String strValue = castString(obj);
if (StringUtils.isNotEmpty(strValue)) {
try {
intValue = Integer.parseInt(strValue);
} catch (NumberFormatException e) {
intValue = defaultValue;
}
}
}
return intValue;
}
/**
* 转换为boolean
* @Title: castBoolean
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param property
* @param @return 设定文件
* @return boolean 返回类型
* @throws
*/
public static boolean castBoolean(Object obj) {
return CastUtil.castBoolean(obj, false);
}
private static boolean castBoolean(Object obj, boolean defaultValue) {
boolean booleanValue = defaultValue;
if (obj != null) {
booleanValue = Boolean.parseBoolean(castString(obj));
}
return booleanValue;
}
}
package com.ghg.smrtframwork.util;
import java.util.Collection;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
public class CollectinoUtil {
/**
* 判断集合是否为空
* @Title: isBlank
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param collection
* @param @return 设定文件
* @return boolean 返回类型
* @throws
*/
public static boolean isEmpty(Collection<?> collection) {
return CollectionUtils.isEmpty(collection);
}
/**
* 非空
* @Title: isNotBlank
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param collection
* @param @return 设定文件
* @return boolean 返回类型
* @throws
*/
public static boolean isNotEmpty(Collection<?> collection) {
return !isEmpty(collection);
}
/**
* 判断map是否为空
* @Title: isEmpty
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param map
* @param @return 设定文件
* @return boolean 返回类型
* @throws
*/
public static boolean isEmpty(Map<?, ?> map) {
return MapUtils.isEmpty(map);
}
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
}
package com.ghg.smrtframwork.util;
import java.io.Closeable;
import java.io.IOException;
public class IOUtil {
public static void close(Closeable closeable){
if(closeable!=null){
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}