package readproperty.v20;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
/**
* <br>TITLE : 读/写属性文件的工具类.
* <br>CREATEUSER : GongQingBao
* <br>CREATEDATE : 2009-3-23
* <br>DESCRIPTION : 可以使用此工具的getMsg(*) / setMsg(**)方法读、写properties文件.
* <br>
* <br>使用步骤:
* <br>1.首先,获得PropertyUtil实例. 使用 静态法 newInstance(**) 获得
* <br> 如: PropertyUtil propUtil =
* PropertyUtil.newInstance("property_name"); // 其中参数"property_name" 为property的文件名.注意 不包含扩展名.
* <br>
* <br>2.使用PropertyUtil实例的(getMsg(String code)
* 或 getMsg(String code, Object[] objs))方法取值即可. setMsg(String key, String value) 用来写.
* <br> 如: propUtil.get("name"); // 获得name对应的值
* <br>
* <br><b>特点: 无论有多少properties文件只需这一个类足矣!
* <br>注意: properties的文件名字不可重复.</b>
*/
public final class PropertyUtil {
public static final int DEFAULT_PATH = 0; // 默认路径
public static final int RELATIVE_PATH = 1; // 相对路径
public static final int ABSOLUTE_PATH = 2; // 绝对路径
private static Object clockObj = PropertyUtil.class;
// 存放参数的property文件的默认位置
private static final String PROPERTY_FILE_PATH = "/";
// 存放每个properties文件对应的PropertyUtil
private static Map propertyUtilMap = new HashMap();
// 记录Timer是否已经启动. true :已经启动;false:未启动
private static boolean timerIsStart = false;
private String filePath = null;
private Properties properties = null;
// 记录文件修改时间
private long modifyTime = 0;
// 临时判断创建是否成功
private static boolean success = false;
/**
* 实例化PropertyUtil对象.
*
* @param propertyName
* 属性文件的名字. 注意不包含扩展名.且属性文件默认在 %classpath%下
* (即:工程的src下).
* @param path
* 文件路径,不包含文件名
* @param bln
* DEFAULT_PATH=0:默认路径;
* RELATIVE_PATH=1:path为相对路径,以%classpath%/为头. 如:
* "com/frame/properties" ; ABSOLUTE_PATH=2:path为绝对路径,如:
* "e:/file"
*/
private PropertyUtil(final String propertyName, final String path,
final int bln) {
try {
success = false;
if (DEFAULT_PATH == bln) {
filePath = this.getClassPath() + propertyName + ".properties";
} else if (RELATIVE_PATH == bln) {
filePath = this.getClassPath() + path + "/" + propertyName + ".properties";
} else if (ABSOLUTE_PATH == bln) {
filePath = path + "/" + propertyName + ".properties";
} else {
System.err.print("属性文件路径类型在规定范围之外! 规定范围: 0、1、2 ");
}
if (null != filePath && !"".equals(filePath)) {
File file = new File(filePath);
if (file.exists()) {
InputStream instream = new FileInputStream(filePath);
this.properties = new Properties();
properties.load(instream);
instream.close();
success = true;
} else {
System.out.println("属性文件不存在! filePath = " + filePath);
}
} else {
System.out.println("属性文件路径为空!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获得ClassPath的路径.
*
* @return String 路径字符串
*/
private String getClassPath() {
URL url = this.getClass().getResource(PROPERTY_FILE_PATH);
String path = null;
try {
path = URLDecoder.decode(url.getPath(), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return path;
}
/**
* 获得指定文件的PropertyUtil.
*
* @param propertyName
* 属性文件的名字. 注意不包含扩展名.
* 且属性文件默认在 %classpath%/下(即:工程的src下).
* @return PropertyUtil
*/
public static PropertyUtil newInstance(final String propertyName) {
return initPropertyUtil(propertyName, "", 0);
}
/**
* 获得指定文件的PropertyUtil. 可以指定文件所在路径
*
* @param propertyName
* 属性文件的名字. 注意不包含扩展名.
* 且属性文件默认在 %classpath%/下(即:工程的src下).
* @param path 文件路径,不包含文件名
* @param bln
* DEFAULT_PATH=0:默认路径;
* RELATIVE_PATH=1:path为相对路径,以%classpath%/为头. 如: "com/frame/properties" ;
* ABSOLUTE_PATH=2:path为绝对路径,如:
* "e:/file"
* @return PropertyUtil
*/
public static PropertyUtil newInstance(final String propertyName, final String path, final int bln) {
return initPropertyUtil(propertyName, path, bln);
}
/**
* 初始化.
*
* @param propertyName
* @param path
* @param bln
* @return PropertyUtil
*/
@SuppressWarnings("unchecked")
private static PropertyUtil initPropertyUtil(final String propertyName,
final String path, final int bln) {
synchronized (clockObj) {
PropertyUtil propertyUtil = (PropertyUtil) propertyUtilMap.get(propertyName);
if (null == propertyUtil) {
propertyUtil = new PropertyUtil(propertyName, path, bln);
if (success) {
propertyUtilMap.put(propertyName, propertyUtil);
}
}
// 启动Timer
if (!PropertyUtil.timerIsStart) {
new Timer().schedule(new CheckFileChange(propertyUtil), 1000, 3000);
timerIsStart = true;
}
return propertyUtil;
}
}
/**
* 获得key对应的value值.
*
* @param key 消息的key
* @return String 消息的key所对应的value
*/
public String getMsg(final String key) {
if (null != properties) {
return properties.getProperty(key);
} else {
return null;
}
}
/**
* 获得key对应的value值. 并具有格式化的功能
*
* @param key
* 消息的key
* @param objs
* 欲插入value中的参数
* @return String 消息的key所对应的value
*/
public String getMsg(final String key, final Object[] objs) {
if (null == properties || null == properties.getProperty(key)) {
return null;
} else {
return MessageFormat.format(properties.getProperty(key), objs);
}
}
/**
* 更新(或插入)一对properties信息(主键及其键值)
* 如果该主键已经存在,更新该主键的值; 如果该主键不存在,则插件一对键值。
*
* @param key
* 键名
*
没有合适的资源?快使用搜索试试~ 我知道了~
Properties文件读写;Property文件读写;Property
共2个文件
java:2个
3星 · 超过75%的资源 需积分: 22 24 下载量 89 浏览量
2011-05-09
14:23:40
上传
评论
收藏 4KB RAR 举报
温馨提示
读/写属性文件的工具类. PropertyUtil.java对Property文件读写进行了封装, 使开发人员对Property文件的读写更加容易。 在性能、实用性 方面还是可以的。
资源推荐
资源详情
资源评论
收起资源包目录
property文件处理_V1.0.rar (2个子文件)
property文件处理_V1.0
TestPropertyUtil.java 2KB
PropertyUtil.java 10KB
共 2 条
- 1
资源评论
- HumanFactory2014-06-26很好 可以使用 谢谢分享
- ASoft阿思2013-01-13读取properties文件工具类,可以使用
gongqingbao_lanxin
- 粉丝: 0
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功