您的位置:首页 > 编程语言 > Java开发

Java配置文件Properties的读取、写入与更新操作

2016-02-17 17:49 399 查看
    在做中央库管理系统时经理要求我写一个关于配置文件的读写与更新的功能,刚开始以为很简单,可是在写的时候不同情况下文件路径的写法以及读取时缓存问题整的我晕头转向,花了好长时间才调试好,这里针对配置文件读写与更新时不同情况下文件路径的写法、读取时缓存问题做个总结,以备下次参考。

    java.util.Properties类表示了一个持久的属性集。
Properties
可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。可对.properties和.xml文件进行操作,其具体方法如下:【详情可以参考JDK API】





下面以.properties文件为例,总结下Properties配置文件的读写及更新

    如果对配置文件只是读取,没有写入和更新操作,就不用考虑读取的value是否是最新的值,即不用考虑读取时缓存问题,如果要写入或更新文件,此种情况下需要考虑缓存问题。.properties文件的读写可以用Properties类中的
load(InputStream inStream)
方法并结合读取File方法来实现该功能,可使用两种情况来实现:

1、读、写项目中的.properties文件 

 (1)如果只是读取项目中的文件,可以 
load(InputStream inStream)
方法结合this.getClass().getResourceAsStream(file)方法读取,getResourceAsStream()方法读取新文件后会被java虚拟机缓存,而再次调用getResourceAsStream()方法时会先查找java虚拟机中是否有此文件,如果有则直接返回,如果没有才会去根据传入的name获取文件。代码如下:

/**
* @PropertiesUtils.java
* @description : properties工具类
* @author LYF 2016-01-26
* @version 1.0
*
*/
public class PropertiesUtils {
/**
* 加载属性文件
* @param filePath 文件路径
* @return
*/
public static Properties loadProps(String filePath){
Properties properties = new Properties();
try {
<span style="white-space:pre">		</span>properties.load(UtilProperties.class.getResourceAsStream(filePath));
} catch (Exception e) {
e.printStackTrace();
}
return properties;
}

/**
* 读取配置文件
* @param props 配置文件
* @param key
* @return
*/
public static String getString(Properties properties,String key){
return properties.getProperty(key);
}
}
<pre name="code" class="java">调用方法:
Properties properties = PropertiesUtils.loadProps("/properties/login_error.properties"); //绝对路径
或者 Properties properties = PropertiesUtils.loadProps("../../../../login_error.properties"); //相对路径
properties.getString(properties,key);//根据properties,key读取value

(2)如果读、写、更新项目中的文件,不能用(1)中的方法,因为getResourceAsStream(file)会先查找java虚拟机中是否有此文件,如果有,直接从缓存中读,而此时如果更新了此文件,用getResourceAsStream()方法读取的仍然是缓存中的信息而不是更新后的信息。此时可以用BufferedInputStream、FileOutputStream来读取,代码如下:

public class PropertiesUtils {
private static Properties properties;
/**
* 加载属性文件
* @param filePath 文件路径
* @return
*/
public static Properties loadProps(String filePath){
properties = new Properties();
try {
String path = UtilProperties.class.getResource(filePath).getPath();
InputStream in =new BufferedInputStream(new FileInputStream(path));
properties.load(in);
} catch (Exception e) {
e.printStackTrace();
}
return properties;
}

/**
* 读取配置文件
* @param props 配置文件
* @param key
* @return
*/
public static String getString(Properties properties,String key){
return properties.getProperty(key);
}

/**
* 更新配置文件
* @param props 配置文件
* @param key
* @return
*/
public static void updatePropertyLoginError(Properties properties,String filePath,String keyname,String keyvalue) {
try {
properties.setProperty(keyname, keyvalue);
String path = UtilProperties.class.getResource(filePath).getPath();
FileOutputStream outputFile = new FileOutputStream(path);
properties.store(outputFile, "modify");
outputFile.flush();
outputFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}


2、读、写磁盘中的.properties文件 

 
load(InputStream inStream)
方法结合BufferInputStream、FileInputStream方法

/**
* 加载属性文件
* @param filePath 文件路径
* @return
*/
public static Properties loadProps(String filePath){
Properties properties = new Properties();
try {
InputStream in =new BufferedInputStream(new FileInputStream(filePath));
properties.load(in);
} catch (Exception e) {
e.printStackTrace();
}
return properties;
}

/**
* 读取配置文件
* @param props
* @param key
* @return
*/
public static String getString(Properties properties,String key){
return properties.getProperty(key);
}

/**
* 更新properties文件的键值对
* 如果该主键已经存在,更新该主键的值;
* 如果该主键不存在,则插入一对键值。
* @param keyname 键名
* @param keyvalue 键值
*/
public static void updateProperty(Properties properties,String filePath,String keyname,String keyvalue) {
try {

// 从输入流中读取属性列表(键和元素对)
properties.setProperty(keyname, keyvalue);
FileOutputStream outputFile = new FileOutputStream(filePath);
properties.store(outputFile, null);
outputFile.flush();
outputFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  properties