用单例模式实现读取xxx.properties文件的内容
2010-09-29 21:14
429 查看
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import org.apache.log4j.Logger;
/**.
* 鍔熻兘:OVLoadProperties
* @author
*
*/
public class OVLoadProperties {
private static Logger logger = Logger.getLogger(OVLoadProperties.class);
/**
* @param map HashMap
*/
private static HashMap<String, String> map;
/**
* @param instance OVLoadProperties
*/
private static OVLoadProperties instance = null;
/**.
* 鍔熻兘:OVLoadProperties
*/
protected OVLoadProperties() {
}
/**.
* 鍔熻兘:getInstance
* @return OVLoadProperties
*/
public static OVLoadProperties getInstance() {
if (instance == null) {
map = new HashMap();
instance = new OVLoadProperties();
return instance;
}
return instance;
}
/**.
* 鍔熻兘:getProperties
* @param key String
* @return String
*/
public String getProperties(String key) {
return getProperties("xxx.properties", key);
}
/**.
* 鍔熻兘:getProperties
* @param config_file String
* @param key String
* @return String
*/
public String getProperties(String config_file, String key) {
if (map.containsKey(key) == false) {
doGet(config_file, key);
}
return (String) map.get(key);
}
/**.
* 鍔熻兘:doGet
* @param key String
*/
// private void doGet(String key) {
// doGet("config.properties", key);
// }
/**.
* 鍔熻兘:doGet
* @param key String
* @param config_file String
*/
private void doGet(String config_file, String key) {
String a = loadProperties(config_file, key);
map.put(key, a);
}
/**.
* 鍔熻兘:loadProperties
* @param key String
* @return String
*/
// private String loadProperties(String key) {
// String config_file = "xxx.properties";
// return loadProperties(config_file, key);
// }
/**.
* 鍔熻兘:loadProperties
* @param key String
* @param config_file String
* @return String
*/
private String loadProperties(String config_file, String key) {
String result = null;
InputStream is = null;
try {
// support the classpath which including space.
// please DO NOT set "." in your system classpath.
Properties prop = new Properties();
is = OVLoadProperties.class.getClassLoader().getResourceAsStream(config_file);
if(is == null){
is = new FileInputStream(config_file);
}
prop.load(is);
result = prop.getProperty(key);
} catch (Exception e) {
OVLog.writeErrorLog(this.getClass(), e,
"loadProperties failure!", "","");
}finally{
if(is!=null){
try
{
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
logger.error("[OVLoadProperties:loadProperties]is close fail");
}
}
}
return result;
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import org.apache.log4j.Logger;
/**.
* 鍔熻兘:OVLoadProperties
* @author
*
*/
public class OVLoadProperties {
private static Logger logger = Logger.getLogger(OVLoadProperties.class);
/**
* @param map HashMap
*/
private static HashMap<String, String> map;
/**
* @param instance OVLoadProperties
*/
private static OVLoadProperties instance = null;
/**.
* 鍔熻兘:OVLoadProperties
*/
protected OVLoadProperties() {
}
/**.
* 鍔熻兘:getInstance
* @return OVLoadProperties
*/
public static OVLoadProperties getInstance() {
if (instance == null) {
map = new HashMap();
instance = new OVLoadProperties();
return instance;
}
return instance;
}
/**.
* 鍔熻兘:getProperties
* @param key String
* @return String
*/
public String getProperties(String key) {
return getProperties("xxx.properties", key);
}
/**.
* 鍔熻兘:getProperties
* @param config_file String
* @param key String
* @return String
*/
public String getProperties(String config_file, String key) {
if (map.containsKey(key) == false) {
doGet(config_file, key);
}
return (String) map.get(key);
}
/**.
* 鍔熻兘:doGet
* @param key String
*/
// private void doGet(String key) {
// doGet("config.properties", key);
// }
/**.
* 鍔熻兘:doGet
* @param key String
* @param config_file String
*/
private void doGet(String config_file, String key) {
String a = loadProperties(config_file, key);
map.put(key, a);
}
/**.
* 鍔熻兘:loadProperties
* @param key String
* @return String
*/
// private String loadProperties(String key) {
// String config_file = "xxx.properties";
// return loadProperties(config_file, key);
// }
/**.
* 鍔熻兘:loadProperties
* @param key String
* @param config_file String
* @return String
*/
private String loadProperties(String config_file, String key) {
String result = null;
InputStream is = null;
try {
// support the classpath which including space.
// please DO NOT set "." in your system classpath.
Properties prop = new Properties();
is = OVLoadProperties.class.getClassLoader().getResourceAsStream(config_file);
if(is == null){
is = new FileInputStream(config_file);
}
prop.load(is);
result = prop.getProperty(key);
} catch (Exception e) {
OVLog.writeErrorLog(this.getClass(), e,
"loadProperties failure!", "","");
}finally{
if(is!=null){
try
{
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
logger.error("[OVLoadProperties:loadProperties]is close fail");
}
}
}
return result;
}
}
相关文章推荐
- java读取resource下xxx.properties配置文件内容
- AntisHsu studying 通过读取.properties配置文件中的键值来实现动态添加注解内容例如@Controller @RequestMapping(value = "${adminPa
- linux下c语言实现tail -f功能---实时读取变化文件中的增量内容
- 读取properties文件内容
- JAVA读取properties文件代码 单例模式
- XXX.properties文件读取
- python计算文件的行数和读取某一行内容的实现方法
- spring注解的方式读取properties文件中的内容
- 五种方式让你在java中读取properties文件内容不再是难题
- 读取文件内容存到数组的具体代码实现
- 纯JS实现的读取excel文件内容功能示例【支持所有浏览器】
- 分别用Java和JS读取Properties文件内容
- struts2 action中读取properties文件内容
- 关于如何使用properties读取配置文件内容问题
- springMvc 如何通过注解@Value 读取配置文件中的内容(.properties)
- java web开发实现properties文件的读取与解析
- Spring 读取properties文件内容
- 读取文件的每一行内容并输出 的shell脚本实现
- Java远程实现Linux文件内容读取.
- 使用js实现读取csv文件内容解析到html页面中