Java(45):java 更新properties配置文件

今天需要需要更新工程中的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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁宁可可

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值