java properties util_java.util.Properties工具类

该博客介绍了一个Java工具类,用于加载、获取、更新和保存属性配置文件。类中包含静态方法获取配置文件对象,更新指定属性并保存到磁盘,以及根据文件名和属性名获取属性值。示例代码展示了如何使用该工具类。

importjava.io.FileNotFoundException;importjava.io.FileWriter;importjava.io.IOException;importjava.io.InputStream;importjava.util.HashMap;importjava.util.LinkedList;importjava.util.List;importjava.util.Map;importjava.util.Properties;/*** @Description 加载属性配置文件工具类

* @Package com.viathink.utils.PropertiesFilesUtils.java

* @date 2012-5-16 下午3:18:19

*@versionV1.0*/

public classPropertiesFilesUtils {private static final PropertiesFilesUtils propertiesFiles = newPropertiesFilesUtils();/*** 存放所有的属性配置文件对象*/

private Map propertiesMap = new HashMap();privatePropertiesFilesUtils() {

}/*** 获得属性配置文件工具类的实例对象

*

*@returnPropertiesFilesUtils 对象*/

public staticPropertiesFilesUtils getInstance() {returnpropertiesFiles;

}/*** 获取Properties文件对象

*

*@paramPropertiesFileName

*@return*@throwsIOException*/

public synchronized Properties getProperties(String propertiesFileName) throwsIOException {if (propertiesFileName == null || "".equals(propertiesFileName)) {throw new IllegalArgumentException("The propertiesFileName is illegal argument!");

}

Properties prop=propertiesMap.get(propertiesFileName);if (prop == null) {

prop= newProperties();//注意这个文件的InputStream,这里要修改

InputStream is = this.getClass().getResourceAsStream(propertiesFileName);if (is == null)throw new FileNotFoundException("The file \"" + propertiesFileName + "\" is not found!");

prop.load(is);

propertiesMap.put(propertiesFileName, prop);

}returnprop;

}/*** 将properties文件写到磁盘上

*

*@paramproperties

* 属性文件

*@parampropertiesFilePath

* 要保存的路径

*@throwsIOException*/

public void writePropertiesFile(Properties properties, String propertiesFilePath) throwsIOException {if (properties == null || propertiesFilePath == null || "".equals(propertiesFilePath))throw new IllegalArgumentException("The props or propertiesFilePath is illegal argument!");

FileWriter fw= newFileWriter(propertiesFilePath);

properties.store(fw,null);

fw.flush();

fw.close();

}/*** 更新属性文件的某个属性,并保存到磁盘上

*

*@paramproperties

* 要更新的属性文件

*@parampropertyName

* 属性名称

*@parampropertyValue

* 属性值

*@paramfilePath

* 保存的文件路径

*@throwsIOException*/

public voidupdatePropertiesFile(Properties properties, String propertyName, String propertyValue, String filePath)throwsIOException {if (properties == null || propertyName == null || "".equals(propertyName) || propertyValue == null

|| "".equals(propertyValue) || filePath == null || "".equals(filePath))throw new IllegalArgumentException("The parameters is illegal argument!");

properties.setProperty(propertyName, propertyValue);

writePropertiesFile(properties, filePath);

}/*** 根据属性配置文件名称和属性名称获取属性配置值

*

*@parampropertiesFileName

* 属性配置文件名称

*@parampropertyName

* 属性名称

*@return属性值

*@throwsIOException*/

public String getProperty(String propertiesFileName, String propertyName) throwsIOException {

Properties prop=getProperties(propertiesFileName);returnprop.getProperty(propertyName);

}/*** 获得Properties对象列表

*

*@return

*/

public ListgetProperties() {if(propertiesMap.isEmpty())return null;return new LinkedList(propertiesMap.values());

}/*** 获得属性对象Map

*

*@return

*/

public MapgetPropertiesMap() {returnpropertiesMap;

}public static void main(String[] args) throwsIOException {

String realPath="p.txt";

PropertiesFilesUtils pfu=PropertiesFilesUtils.getInstance();

Properties pro=pfu.getProperties(realPath);

Properties pro1=pfu.getProperties(realPath);

Properties pro2=pfu.getProperties(realPath);

Properties pro3=pfu.getProperties(realPath);

System.out.println(pro==pro1);

System.out.println(pro==pro2);

System.out.println(pro==pro3);

System.out.println(pfu.getProperties().size());

System.out.println(pfu.getPropertiesMap().size());

System.out.println(pro.getProperty("a"));

pfu.updatePropertiesFile(pro,"messge.type", "image", realPath);

System.out.println(pro.getProperty("messge.type"));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值