java properties动态修改

本文介绍了Java中动态修改Properties文件的方法。提供了两种方式,一是使用PropertiesConfiguration类添加或修改项并保存;二是通过Properties类读取、设置属性,再将修改内容写入配置文件,还给出了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package utils;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang3.StringUtils;

/**

  • liqi

  • 20190530
    */
    public class PropertyConfigUtil {

    public static void main(String[] args) {
    try {

         //E:\..\target\classes
         //E:\..\target\
         //E:\..\WEB-INF\classes
    
         //获取配置文件路径
         String path = PropertyConfigUtil.class.getClassLoader().getResource("config.properties").getPath();
    

//方法一:
//添加maven 配置


commons-configuration
commons-configuration
1.10

//代码
path= StringUtils.substringBefore(path, “字符串”);
PropertiesConfiguration config = new PropertiesConfiguration(path);
//添加或修改项
config.setProperty(“update_databese”, “false”);
config.save();

//方法二:
// Properties prop = new Properties();
// //读取配置文件
// FileInputStream in = new FileInputStream(path);
// //加载
// prop.load(in);
// //获取
// prop.getProperty(“update_databese”);
// //设置
// prop.setProperty(“update_databese”, “false”);
// in.close();
// //写到配置文件
//ture 开启追加
//false 重置内容,注意:无序写入
// FileOutputStream outputStream = new FileOutputStream(path,true);
//追加项
// prop.store(outputStream, “IS_Update_DB”);
// outputStream.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

摘自:
https://blog.youkuaiyun.com/capmiachael/article/details/81512634
https://www.cnblogs.com/bakari/p/3562244.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值