首页 > 代码库 > 单例模式PropertiesUtil工具类
单例模式PropertiesUtil工具类
单例模式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; /** * 读取配置文件 * <pre> * Description * Created at: 2017年1月18日 下午3:44:33 * </pre> */ public class PropertiesUtil { private Properties properties = null; private ClassLoader oClassLoader = null; private static Map<String, PropertiesUtil> instanceMap = new HashMap<String, PropertiesUtil>(); 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")); } }
本文出自 “ciyo技术分享” 博客,请务必保留此出处http://ciyorecord.blog.51cto.com/6010867/1939386
单例模式PropertiesUtil工具类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。