📄 configuration.java
字号:
/**
*
*/
package com.tongtu.comm.sql;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @author Administrator
*
*/
public class Configuration {
/** 属性。 */
private Properties properties;
/** 属性文件, */
private static final String CONFIG_FILE = "/tongtu.properties";
/** 配置类,Singleton。 */
private final static Configuration cfg = new Configuration();
/**
* @link
* @shapeType PatternLink
* @pattern Singleton
* @supplierRole Singleton factory
*/
/* # private Configuration _configuration; */
/**
* 构造方法。 使用private使此类仅为自己构造。
*/
private Configuration() {
properties = new Properties();
InputStream is = null;
// FileInputStream is = null;//added by Irevin 2003-5-30
try {
// 读属性文件,WEB-INF/classes/skyinn.properties
is = getClass().getResourceAsStream(CONFIG_FILE);
properties.load(is);
} catch (Exception exception) {// catch exception
System.err.println("Can't configuration with Exception of:"
+ exception.getMessage());
} finally {
try {
// 释放资源
if (is != null)
is.close();
} catch (IOException exception) {
System.err
.println("Can't close configuration file inputstream with Exception of:"
+ exception.getMessage());
}// end try free resource
}// end try...catch...
}// end Configuration()
/**
* 取配置类对象。
*
* @return 配置类对象
*/
public static Configuration getInstance() {
return cfg;
}
/**
* * 取属性对象。
*
* @return 当前配置类加载的属性对象
*/
public Properties getPorperties() {
return this.properties;
}
/**
* 根据key取相应的值。
*
* @param key
* 属性文件中的键名
* @return 该键的值
*/
public String getValue(String key) {
String str = "";
try {
// 返回键值
str = toChinese(properties.getProperty(key));
} catch (Exception e) {// catch Excetion
System.err.println("getValue() Exception in Configuration: " + e);
}
if (null == str)
str = "";
return str;
}// end getValue()
/**
* 将给定的字符串转换为中文GBK编码的字符串。
*
* @param str
* 输入字符串
* @return 经GBK编码后的字符串,如果有异常,则返回原编码字符串
*/
public static String toChinese(final String str) {
if (isNullorBlank(str)) {
return str;
}
String retVal = str;
try {
retVal = new String(str.getBytes("ISO8859_1"), "GBK");
} catch (Exception e) {
// log...
}
return retVal;
}// end toChinese()
/**
* 给定字符串是否为空。
*
* @param str
* 需要检查的字符串
* @return 如果为null或"",则返回true,否则为false
*/
public static boolean isNullorBlank(final String str) {
boolean isNorB = false;
if (null == str || 0 >= str.length()) {
isNorB = true;
}
return isNorB;
}// end isNullorBlank()
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -