- 浏览: 579707 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
smilezhouwei:
请问CruiseControl在加载jar包时,由于jar包过 ...
修改CruiseControl的端口 -
zengxuefei:
不起作用啊,有bug
Flex+Java多文件上传 -
lzeus:
模仿的tomcat源码吧?
java事件处理机制(自定义事件)【转】 -
yangbobestone:
...
FreeMarker整合Struts2 -
fddjxllren:
那是因为你没配置事务,楼主的那个主要是针对事务的写法
Spring2.5+JUnit4单元测试
package com.common.util; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Hashtable; import java.util.Properties; import org.apache.log4j.Logger; /** * * 功能描述:动态读取配置文件来加载属性 * <p> * 版权所有: * <p> * 未经本公司许可,不得以任何方式复制或使用本程序任何部分 * * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public class PropertyReader { private static Logger _log = Logger.getLogger(PropertyReader.class); private static Hashtable<String, Properties> pptContainer = new Hashtable<String, Properties>(); /** * * 方法用途和描述: 获得属性 * * @param propertyFilePath * 属性文件(包括类路径) * @param key * 属性键 * @return 属性值 * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static String getValue(String propertyFilePath, String key) { Properties ppts = getProperties(propertyFilePath); return ppts == null ? null : ppts.getProperty(key); } /** * * 方法用途和描述: 获得属性文件中Key所对应的值 * * @param propertyFilePath * 属性文件路径(包括类路径或文件系统中文件路径) * @param key * 属性的键 * @param isAbsolutePath * 是否为绝对路径:true|false〔即是本地文件系统路径,比如:C:/test.propreties〕<br> * <br> * <b>注:</b>不能通过类路径来获取到属性文件,而只知道属性文件的文件系统路径,即文件系统地址则用此方法来获取其中的Key所对应的Value * @return key的属性值 * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static String getValue(String propertyFilePath, String key, boolean isAbsolutePath) { if (isAbsolutePath) { Properties ppts = getPropertiesByFs(propertyFilePath); return ppts == null ? null : ppts.getProperty(key); } return getValue(propertyFilePath, key); } /** * * 方法用途和描述: 获得属性文件的属性 * * @param propertyFilePath * 属性文件(包括类路径) * @return 属性 * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static Properties getProperties(String propertyFilePath) { if (propertyFilePath == null) { _log.error("propertyFilePath is null!"); return null; } Properties ppts = pptContainer.get(propertyFilePath); if (ppts == null) { ppts = loadPropertyFile(propertyFilePath); if (ppts != null) { pptContainer.put(propertyFilePath, ppts); } } return ppts; } /** * * 方法用途和描述: 获得属性文件的属性 * * @param propertyFilePath * 属性文件路径(包括类路径及文件系统路径) * @return 属性文件对象 Properties * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static Properties getPropertiesByFs(String propertyFilePath) { if (propertyFilePath == null) { _log.error("propertyFilePath is null!"); return null; } Properties ppts = pptContainer.get(propertyFilePath); if (ppts == null) { ppts = loadPropertyFileByFileSystem(propertyFilePath); if (ppts != null) { pptContainer.put(propertyFilePath, ppts); } } return ppts; } /** * * 方法用途和描述: 加载属性 * * @param propertyFilePath * 属性文件(包括类路径) * @return 属性 * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ private static Properties loadPropertyFile(String propertyFilePath) { java.io.InputStream is = PropertyReader.class .getResourceAsStream(propertyFilePath); if (is == null) { return loadPropertyFileByFileSystem(propertyFilePath); } Properties ppts = new Properties(); try { ppts.load(is); return ppts; } catch (Exception e) { _log.debug("加载属性文件出错:" + propertyFilePath, e); return null; } } /** * * 方法用途和描述: 从文件系统加载属性文件 * * @param propertyFilePath * 属性文件(文件系统的文件路径) * @return 属性 * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ private static Properties loadPropertyFileByFileSystem( final String propertyFilePath) { try { Properties ppts = new Properties(); ppts.load(new java.io.FileInputStream(propertyFilePath)); return ppts; } catch (java.io.FileNotFoundException e) { _log.error("FileInputStream(\"" + propertyFilePath + "\")! FileNotFoundException: " + e); return null; } catch (java.io.IOException e) { _log.error("Properties.load(InputStream)! IOException: " + e); return null; } } /** * * 方法用途和描述: 对存在的属性文件中添加键值对并保存 * * @param propertyFilePath * 属性文件的路径(包括类路径及文件系统路径) * @param htKeyValue * 键值对Hashtable * @return * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static boolean setValueAndStore(String propertyFilePath, java.util.Hashtable<String, String> htKeyValue) { return setValueAndStore(propertyFilePath, htKeyValue, null); } /** * * 方法用途和描述: 对存在的属性文件中添加键值对并保存 * * @param propertyFilePath * 属性文件的路径(包括类路径及文件系统路径) * @param htKeyValue * 键值对Hashtable * @param storeMsg * 保存时添加的附加信息(注释) * @return * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static boolean setValueAndStore(String propertyFilePath, java.util.Hashtable<String, String> htKeyValue, String storeMsg) { Properties ppts = getProperties(propertyFilePath); if (ppts == null || htKeyValue == null) { return false; } ppts.putAll(htKeyValue); java.io.OutputStream stream = null; try { stream = new java.io.FileOutputStream(propertyFilePath); } catch (FileNotFoundException e) { _log.debug("propertyFilePath = " + propertyFilePath); String path = PropertyReader.class.getResource(propertyFilePath) .getPath(); _log.debug("~~~~~~~~path~~~XXX~~~~~" + path); try { stream = new java.io.FileOutputStream(path); } catch (FileNotFoundException e1) { _log.error("FileNotFoundException! path=" + propertyFilePath); return false; } } if (stream == null) { return false; } try { ppts.store(stream, storeMsg != null ? storeMsg : "set value and store."); return true; } catch (java.io.IOException e) { e.printStackTrace(); return false; } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * * 方法用途和描述: 创建属性文件 * * @param propertyFilePath * 要存储属性文件的路径 * @param htKeyValue * 属性文件中的键值对Hashtable * @return * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static boolean createPropertiesFile(String propertyFilePath, java.util.Hashtable<String, String> htKeyValue) { java.io.File file = new java.io.File(propertyFilePath); if (!file.exists()) { try { file.createNewFile(); } catch (java.io.IOException e) { e.printStackTrace(); } } return setValueAndStore(propertyFilePath, htKeyValue, "create properties file:" + file.getName()); } /** * * 方法用途和描述:设置属性值 * * @param propertyFilePath * 属性文件(包括类路径) * @param key * 属性键 * @param value * 属性值 * @return * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static boolean setValue(String propertyFilePath, String key, String value) { Properties ppts = getProperties(propertyFilePath); if (ppts == null) { return false; } ppts.put(key, value); return true; } /** * * 方法用途和描述: 保存属性文件对象 * * @param properties * 属性文件对象 * @param propertyFilePath * 要保存的路径 * @param msg * 保存时添加的附加信息(注释) * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static void store(Properties properties, String propertyFilePath, String msg) { try { java.io.OutputStream stream = new java.io.FileOutputStream( propertyFilePath); properties.store(stream, msg); } catch (java.io.FileNotFoundException e) { _log.error("FileOutputStream(" + propertyFilePath + ")! FileNotFoundException: " + e); } catch (java.io.IOException e) { _log.error("store(stream, msg)! IOException: " + e); e.printStackTrace(); } } /** * * 方法用途和描述: 删除属性值 * * @param propertyFilePath * 属性文件(包括类路径) * @param key * 属性键 * @return * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static String removeValue(String propertyFilePath, String key) { Properties ppts = getProperties(propertyFilePath); if (ppts == null) { return null; } return (String) ppts.remove(key); } /** * * 方法用途和描述: 删除属性文件中的Key数组所对应的键值对 * * @param propertyFilePath * 属性文件路径(包括类路径及文件系统路径) * @param key * key数组 * @return 属性文件对象 * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static Properties removeValue(String propertyFilePath, String[] key) { if (key == null) { _log.error("key[] is null!"); return null; } Properties ppts = getProperties(propertyFilePath); if (ppts == null) { return null; } for (String strKey : key) { ppts.remove(strKey); } return ppts; } /** * * 方法用途和描述:删除属性文件中的Key数组所对应的键值对,并将属性文件对象持久化(即保存) * * * @param propertyFilePath * 属性文件路径(包括类路径及文件系统路径) * @param key * 属性文件中的key数组 * @return 成功与否(true|false) * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static boolean removeValueAndStore(String propertyFilePath, String[] key) { Properties ppts = removeValue(propertyFilePath, key); if (ppts == null) { return false; } store(ppts, propertyFilePath, "batch remove key value!"); return true; } /** * * 方法用途和描述: 更新指定路径的属性文件中的键所对应的值 * * @param propertyFilePath * 属性文件路径(包括类路径及文件系统路径) * @param key * 属性文件中的key * @param newValue * 要更新的新值 * @return 成功与否(true|false) * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static boolean updateValue(String propertyFilePath, String key, String newValue) { if (key == null || newValue == null) { _log.error("key or newValue is null!"); return false; } java.util.Hashtable<String, String> ht = new java.util.Hashtable<String, String>(); ht.put(key, newValue); return setValueAndStore(propertyFilePath, ht, "update " + key + "'s value!"); } /** * * 方法用途和描述: 批量更新指定路径的属性文件中的键所对应的值 * * @param propertyFilePath * 属性文件路径(包括类路径及文件系统路径) * @param htKeyValue * 要更新的键值对Hashtable * @return 成功与否(true|false) * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static boolean batchUpdateValue(String propertyFilePath, java.util.Hashtable<String, String> htKeyValue) { if (propertyFilePath == null || htKeyValue == null) { return false; } return setValueAndStore(propertyFilePath, htKeyValue, "batch update key value!"); } /** * * 方法用途和描述: 移除加载的属性文件 * * @param propertyFilePath * 属性文件(包括类路径) * @return * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static Properties removePropertyFile(String propertyFilePath) { return pptContainer.remove(propertyFilePath); } /** * * 方法用途和描述: 重新加载某个Property文件 * * @param propertyFilePath * 要重新加载的Property文件,如果当前内存中没有的话则加载,否则替换 * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static void reloadPropertyFile(String propertyFilePath) { pptContainer.remove(propertyFilePath); loadPropertyFile(propertyFilePath); } /** * * 方法用途和描述: 获得属性文件的路径 * * @param pkg * 包名 * @param propertyFileName * 属性文件名 * @return * @author dengcd 新增日期:2008-10-9 * @author 你的姓名 修改日期:2008-10-9 * @since wapportal_manager version(2.0) */ public final static String getPpropertyFilePath(String pkg, String propertyFileName) { pkg = pkg == null ? "" : pkg.replaceAll("\\.", "/"); propertyFileName = propertyFileName.endsWith(".properties") ? propertyFileName : (propertyFileName + ".properties"); return "/" + pkg + "/" + propertyFileName; } public static void main(String[] args) { String path = "/config/jdbc.properties"; String v = PropertyReader.getValue(path, "jdbc.driverClassName"); _log.info("value0 = " + v); Hashtable<String, String> ht = new Hashtable<String, String>(); ht.put("name", "dengcd"); PropertyReader.setValueAndStore(path, ht); String v_ = PropertyReader.getValue(path, "name"); _log.info("value1 = " + v_); PropertyReader.reloadPropertyFile(path); String v2_ = PropertyReader.getValue(path, "name"); _log.info("value2 = " + v2_); } }
发表评论
-
Java枚举使用
2010-06-07 10:57 1131Java枚举使用 public class MyT ... -
在ssh、telnet断开之后继续执行程序
2010-05-28 13:54 6309在ssh、telnet断开之后继 ... -
Resin Web容器下服务初始化了两次
2010-05-26 10:01 3587Resin Web容器下服务初始化了两次: 问题描 ... -
java事件处理机制(自定义事件)【转】
2010-04-01 09:52 10176java事件处理机制(自 ... -
配置多个事务
2010-03-22 12:02 883有关配置多个数据源及事务管理时要注意在执行操作时要指明事务 ... -
Java缩放生成新的图片
2009-10-14 11:31 1261缩放生成新的图片,需要附件中的gif4j.jar支持 ... -
用Rome解析RSS
2009-09-03 13:58 7479用Rome解析Rss的例子 需要的jar包 ... -
Java 注解(Annotation)
2009-08-14 12:04 1243注解(Annotation) 为我们在代码中天界信息提供了一 ... -
Java类中数据以JSON格式输出结果
2009-06-17 10:16 14453/** * 以格式输出结果 * ... -
JAVA字符串转日期或日期转字符串
2009-06-10 16:31 1282文章中,用的API是SimpleDateFormat,它 ... -
通过Java反射调用方法
2009-05-25 13:59 1798通过Java反射调用方法 这是个测试用的例子,通过反射 ... -
Java中对数据库的事务控制
2009-04-03 09:40 1279try{ conn.setAutoCommit(false ... -
Cookie管理
2009-04-03 09:39 1046package com.yimei.util; import ... -
Servlet中输出为XML工具类
2009-03-11 15:54 1184在工具类中定义如下方法: protected void r ... -
Java中取sql语句中的列
2009-02-16 17:21 2276package com.test; import java.s ... -
Java反射
2009-02-13 03:27 1061package com.yimei.test; import ... -
HttpClient入门
2009-02-11 12:52 1972developerWorks 中国 > ... -
Java中的类反射
2009-01-15 11:36 1395一、反射的概念 : 反射的概念是由Smith在1982年首次提 ... -
Java序列化
2009-01-14 09:43 1123package test; import java.io.F ... -
POI导出Excel文件
2009-01-06 11:39 4326HSSFWorkbook wb = new H ...
相关推荐
我们要做的第一步是要将文件读取到Properties类对象中,由于load有一个参数是InputStream,所以我们可以用 InputStream的子类FileInputStream将属性文件读取到Properties对象中,知道prop.properties的路径,我们...
读/写属性文件的工具类. PropertyUtil.java对Property文件读写进行了封装, 使开发人员对Property文件的读写更加容易。 在性能、实用性 方面还是可以的。
Properties操作工具类(ToolProperties.java),读写Properties文件操作 网络操作工具类(ToolNetwork.java),获取网络信息、更改切换网络等相关操作 日期操作工具类(ToolDateTime.java),获取日期、日期加减、格式...
Properties操作工具类(ToolProperties.java),读写Properties文件操作 网络操作工具类(ToolNetwork.java),获取网络信息、更改切换网络等相关操作 日期操作工具类(ToolDateTime.java),获取日期、日期加减、格式...
Properties操作工具类(ToolProperties.java),读写Properties文件操作 网络操作工具类(ToolNetwork.java),获取网络信息、更改切换网络等相关操作 日期操作工具类(ToolDateTime.java),获取日期、日期加减、...
Properties操作工具类(ToolProperties.java),读写Properties文件操作 网络操作工具类(ToolNetwork.java),获取网络信息、更改切换网络等相关操作 日期操作工具类(ToolDateTime.java),获取日期、日期加减、...
..\辅助工具类 ..\..........\util ..\..........\....\CastorHandler.java ..\..........\....\DbUtil.java ..\..........\....\Debug.java ..\..........\....\FileUtil.java ..\..........\....\...
实例147 随机读写Java类文件 221 第3篇 Java面向对象编程 第8章 面向对象(教学视频:72分钟) 226 8.1 类 226 实例148 简单的通讯录类 226 实例149 简单的长度单位转换类 227 实例150 ...
实例147 随机读写Java类文件 221 第3篇 Java面向对象编程 第8章 面向对象(教学视频:72分钟) 226 8.1 类 226 实例148 简单的通讯录类 226 实例149 简单的长度单位转换类 227 实例150 卡车和卡车司机之间的关系 229...
实例147 随机读写Java类文件 221 第3篇 Java面向对象编程 第8章 面向对象(教学视频:72分钟) 226 8.1 类 226 实例148 简单的通讯录类 226 实例149 简单的长度单位转换类 227 实例150 卡车和...
实例147 随机读写Java类文件 221 第3篇 Java面向对象编程 第8章 面向对象(教学视频:72分钟) 226 8.1 类 226 实例148 简单的通讯录类 226 实例149 简单的长度单位转换类 227 实例150 卡车和卡车司机之间的...
实例147 随机读写Java类文件 221 第3篇 Java面向对象编程 第8章 面向对象(教学视频:72分钟) 226 8.1 类 226 实例148 简单的通讯录类 226 实例149 简单的长度单位转换类 227 实例150 卡车和卡车司机之间的关系 229...
13.2.3如何获取类中的成员信息 13.3通过反射机制访问对象 13.3.1如何实例化对象 13.3.2如何给成员变量赋值 13.3.3如何调用成员函数 13.4何时使用反射 13.5动态异常处理框架 13.5.1框架功能简介 13.5.2重要...
目录结构 downloader(网页请求处理工具类) httpclientDownloader apache的Httpclient包封装网页下载器 JsoupDownloader Jsoup封装下载,支持get,post,设置header和...PropertiesUtils 获取java Properties装备对象
常用编码工具类,支持base64,md5,des,crc32 也谈谈技术面试 在C#里把ArrayList转换为Array 或 把Array转换为ArrayList C# 2.0 在.NET 2.0中,让你的组件也可以绑定 .NET20 一种简单的窗口控件UI状态控制方法 翻译MSDN...
飞趣社区开源版使用了hutool工具类作支撑,同时参考了zheng项目以及ruoyi的项目。页面ui使用了layui,集成了阿里云oss(支持前端和后端上传)、七牛云,支持代码自动生成,支持数据库读写分离,减去了70%的工作量,...
Spring+SpringMVC+Mybatis框架集成公共模块,包括公共配置、MybatisGenerator扩展插件、通用BaseService、工具类等。 > zheng-admin 基于bootstrap实现的响应式Material Design风格的通用后台管理系统,`zheng`...
2.2.2 struts 2属性配置文件struts.properties详解 55 2.2.3 struts 2核心配置文件struts.xml详解 57 2.3 struts 2应用开发实务 61 2.3.1 struts 2应用开发环境的搭建 62 2.3.2 struts 2应用基本开发步骤—搭建...
随后我们在resources文件夹中新建一个redis.properties,往里面添加如下内容。 再新建一个spring-redis.xml,往里面添加我们redis相关的配置。其中maxInactiveIntervalInSeconds是设置session有效时间,以秒为...