其他service 单例 工具类_单例模式PropertiesUtil工具类

这是一个Java实现的PropertiesUtil工具类,采用单例模式,用于读取、修改配置文件中的属性值。类中包含了加载、获取和设置属性值的方法,并提供了异常处理。在主函数中展示了如何使用该工具类修改和读取配置文件内容。
摘要由CSDN通过智能技术生成

单例模式PropertiesUtil工具类import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

/**

* 读取配置文件

 
 

* Description

* Created at: 2017年1月18日 下午3:44:33

*/

public class PropertiesUtil {

private Properties properties = null;

private ClassLoader oClassLoader = null;

private static Map instanceMap = new HashMap();

private String propertyFileName;

private Log log = LogFactory.getLog(PropertiesUtil.class);

//定义私有构造方法(防止通过 new PropertiesUtil()去实例化)

private PropertiesUtil(String propertyFileName) {

this.propertyFileName = propertyFileName;

loadProperties();

}

/**

* 获取单例对象

* @param propertyFileName 文件名称

* @return

*/

public static PropertiesUtil getInstance(String propertyFileName) {

if (instanceMap.get(propertyFileName) != null) {

return (PropertiesUtil) instanceMap.get(propertyFileName);

}

//实例化

PropertiesUtil instance = new PropertiesUtil(propertyFileName);

instanceMap.put(propertyFileName, instance);

return instance;

}

/**

* 加载properties文件

*/

private void loadProperties(){

try {

this.properties = new Properties();

this.oClassLoader = Thread.currentThread().getContextClassLoader();

InputStream is = oClassLoader.getResourceAsStream(this.propertyFileName + ".properties");

if (is != null) {

this.properties.load(is);

is.close();

}

is = null;

} catch (Exception e) {

this.log.error("找不到指定的文件,查看文件名是否正确:" + propertyFileName + e.getMessage(), e);

}

}

/**

* 获取文件属性值

* @param propertyName 属性名

* @param defaultValue 默认值

* @return

*/

public String getPropertyAsString(String propertyName, String defaultValue) {

try {

if (this.properties == null) {

loadProperties();

}

return this.properties.getProperty(propertyName, defaultValue);

} catch (Exception e) {

log.error(propertyName + "属性文件读取错误" + e.getMessage(), e);

}

return defaultValue;

}

/**

* 获取String类型的文件属性值

* @param propertyName

* @return

*/

public String getPropertyAsString(String propertyName) {

return getPropertyAsString(propertyName, null);

}

/**

* 获取Int类型的文件属性值

* @param propertyName

* @param defaultValue 默认值

* @return

*/

public int getPropertyAsInt(String propertyName, int defaultValue) {

try {

if (this.properties == null) {

loadProperties();

}

String sProperty = this.properties.getProperty(propertyName);

return Integer.parseInt(sProperty);

} catch (Exception e) {

log.error(propertyName + "属性文件读取错误" + e.getMessage(), e);

}

return defaultValue;

}

/**

* 获取Int类型的文件属性值

* @param propertyName

* @return

*/

public int getPropertyAsInt(String propertyName) {

return getPropertyAsInt(propertyName, 0);

}

/**

* 修改文件属性值

* @param propertyName 属性名

* @param propertyValue 属性值

*/

public void setProperty(String propertyName, String propertyValue) {

try {

if (this.properties == null) {

loadProperties();

}

String filePath = String.valueOf(this.oClassLoader.getResource("")) + this.propertyFileName + ".properties";

filePath = filePath.replaceAll("file:/", "");

filePath = filePath.replaceAll("%20", " ");

if (filePath.indexOf(":") != 1) {

filePath = File.separator + filePath;

}

OutputStream fos = new FileOutputStream(filePath);

properties.setProperty(propertyName, propertyValue);

properties.store(fos, "Update ‘" + propertyName + "‘ value");

if (fos != null) {

fos.close();

}

fos = null;

} catch (Exception e) {

log.error(propertyName + "属性文件更新错误" + e.getMessage(), e);

}

}

public static void main(String[] args) {

PropertiesUtil.getInstance("system_config").setProperty("IS_ZHUCE", "no");

System.out.println(">>>>>" + PropertiesUtil.getInstance("system_config").getPropertyAsString("IS_ZHUCE"));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值