package project.wallet;
|
|
import java.io.InputStream;
|
import java.io.UnsupportedEncodingException;
|
import java.util.Properties;
|
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
/**
|
* 读取Properties综合类,默认绑定到classpath下的config.properties文件。
|
*/
|
public class PropertiesUtilWallet {
|
private static final Logger logger = LoggerFactory.getLogger(PropertiesUtilWallet.class);
|
private static String CONFIG_FILENAME = "config/wallet.properties";
|
private static Properties prop = null;
|
|
public PropertiesUtilWallet() {
|
if (prop == null) {
|
loadProperties();
|
}
|
};
|
|
private synchronized static void loadProperties() {
|
byte buff[]=null;
|
try {
|
//Open the props file
|
InputStream is=PropertiesUtilWallet.class.getResourceAsStream("/" + CONFIG_FILENAME);
|
prop = new Properties();
|
//Read in the stored properties
|
prop.load(is);
|
}
|
catch (Exception e) {
|
System.err.println("读取配置文件失败!!!");
|
prop = null;
|
logger.error(e.getMessage(), e);
|
}
|
}
|
|
/**
|
* 得到属性值
|
* @param key
|
* @return
|
*/
|
public static String getProperty(String key) {
|
if (prop == null) {
|
loadProperties();
|
}
|
|
String value = prop.getProperty(key);
|
if(value ==null){
|
return null;
|
}
|
return value.trim();
|
}
|
|
/**
|
* 得到内容包含汉字的属性值
|
* @param key
|
* @return
|
*/
|
public static String getGBKProperty(String key) {
|
String value = getProperty(key);
|
try {
|
value = new String(value.getBytes("ISO8859-1"),"GBK");
|
} catch (UnsupportedEncodingException e) {
|
}
|
|
if(value ==null){
|
return null;
|
}
|
return value.trim();
|
}
|
|
/**
|
* 得到属性值,
|
* @param key
|
* @param defaultValue
|
* @return
|
*/
|
public static String getProperty(String key, String defaultValue) {
|
if (prop == null) {
|
loadProperties();
|
}
|
|
String value = prop.getProperty(key, defaultValue);
|
if(value ==null){
|
return null;
|
}
|
return value.trim();
|
}
|
|
/**
|
* 得到内容包含汉字的属性值,如果不存在则使用默认值
|
* @param key
|
* @return
|
*/
|
public static String getGBKProperty(String key, String defaultValue) {
|
try {
|
defaultValue = new String(defaultValue.getBytes("GBK"), "ISO8859-1");
|
String value = getProperty(key, defaultValue);
|
value = new String(value.getBytes("ISO8859-1"), "GBK");
|
|
if (value == null) {
|
return null;
|
}
|
return value.trim();
|
} catch (UnsupportedEncodingException e) {
|
return null;
|
}
|
}
|
|
public static String getUTFProperty(String key, String defaultValue) {
|
try {
|
defaultValue = new String(defaultValue.getBytes("UTF-8"),
|
"ISO8859-1");
|
String value = getProperty(key, defaultValue);
|
value = new String(value.getBytes("ISO8859-1"), "UTF-8");
|
|
if (value == null) {
|
return null;
|
}
|
return value.trim();
|
} catch (UnsupportedEncodingException e) {
|
return null;
|
}
|
}
|
|
public static void main(String[] args) {
|
System.out.println(PropertiesUtilWallet.getProperty("mail.username"));
|
}
|
}
|