今天需要需要更新工程中的properties配置文件的已有数据,发现重新写入的数据顺序变乱了,而且有中文乱码问题。
第一种方法
1、基于java.util.properties(配置文件会乱,不推荐使用)
优点在于无需引入额外的jar包,但有一个明显的缺点:通过这种方式修改的properties文件,其键值对顺序会乱
/**
* @function updateProperties更新配置文件(但是写入会乱,抛弃)
*/
public static Properties updateProperties(String filename,String key, String value) throws Exception {
Properties pro = new Properties();
String enConding = "UTF-8";
if (isWindows())
enConding = "GBK";
try{
//当前工作目录
String path = System.getProperty("user.dir");
String filePath = path + "/src/main/resources/"+filename;
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), enConding));
pro.load(br);
OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(filePath), enConding);
pro.setProperty(key, value);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
pro.store(oStreamWriter, "Update '" + key + "' value");
br.close();
oStreamWriter.close();
}
catch (Exception e) {
e.printStackTrace();
log.error("更新Properties错误"+key);
}
return pro;
执行更新后结果:
String dbFile="db.properties";
Common.updateProperties(dbFile,"taskId","1449990615931584000");
使用该方法写配置文件有一弊处:导致配置文件里面的配置项变得混乱。
https://www.cnblogs.com/pijunqi/p/14688734.html
参考:
PropertiesConfiguration的用法 - 小虾米的java梦 - 博客园
https://www.cnblogs.com/tian874540961/p/12146467.html
https://www.jb51.net/article/197130.htm
https://www.iteye.com/blog/happyqing-1966014
第二种方式(更新键值)
基于apache.commons.configuration.PropertiesConfiguration
这种方式不会造成写入的键值对顺序紊乱(配置文件里面参数多的情况下极力推荐),不过需要引入commons-configuration2包
这个方式也有问题是:原有配置文件中的value值如果有中文会乱码,值没有中文没有问题。
Maven引用包:
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-configuration2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.7</version>
</dependency>
<!-- commons-lang3依赖-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
使用代码:
/**
* @function updateProperty更新配置文件(但是写入value中中文乱码,未解决)
* commons.configuration2 第三方包
*/
public static void updateProperty(String properties, String key, String newValue) {
String path = System.getProperty("user.dir");
String filePath = path + "/src/main/resources/" + properties;
String enConding = "UTF-8";
if (isWindows())
enConding = "GBK";
File file = new File(filePath);
PropertiesConfiguration config = new PropertiesConfiguration();
PropertiesConfigurationLayout layout = config.getLayout();
try {
layout.load(config, new InputStreamReader(new FileInputStream(file), "GBK"));
config.setProperty(key, newValue);
layout.save(config,new OutputStreamWriter(new FileOutputStream(file,false), "GBK"));
} catch (Exception e) {
e.printStackTrace();
log.error(e);
}
}
参考:
使用apache commons configuration代替java.util.Properties写配置文件_蜡笔小达新的博客-CSDN博客
更新配置文件操作:https://www.cnblogs.com/gavinwangxj/p/7112115.html
PropertiesConfiguration的用法 - 小虾米的java梦 - 博客园
如果要解决commons-configuration中文乱码问题,可以参考:
参考:https://blog.csdn.net/10km/article/details/78670921