首页 > 其他分享 >springboot动态修改properties文件

springboot动态修改properties文件

时间:2023-03-22 14:56:43浏览次数:29  
标签:java String io fileName 修改 key import properties springboot

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URLDecoder;
import java.util.Properties;

import org.apache.logging.log4j.util.PropertiesUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class OperationProperties {

    /***
     * 动态获取配置文件内容
     * @param fileName 配置文件名称
     * @param key 配置文件key值
     * @return 返回对应key的值
     */
    public static String getProperties(String fileName,String key) {
        String ret= "";
        try {
            retKeyVal = PropertiesLoaderUtils.loadAllProperties(fileName).get(key).toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /***
     * 动态修改配置文件并返回修改值
     * @param fileName
     * @param key
     * @param value
     * @return value
     */
    public static String updateProperties(String fileName,String key,String value) {
        try {
            String filePath = PropertiesUtil.class.getClassLoader().getResource(fileName).getFile();
            filePath = URLDecoder.decode(filePath, "utf-8");
            Properties props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(fileName));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
            props.setProperty(key, value);
            props.store(bw, "");
        } catch (Exception e) {

            e.printStackTrace();
        }
        return value;
    }

}

 修改的是运行时的配置文件(target)

标签:java,String,io,fileName,修改,key,import,properties,springboot
From: https://www.cnblogs.com/qtfwax/p/17219930.html

相关文章

  • 修改软键盘上“换行”文本
    https://blog.csdn.net/weixin_44169843/article/details/128454598<inputenterkeyhint="enter"><inputenterkeyhint="done"><inputenterkeyhint="go"><inputenter......
  • tomcat中修改jvm的,编码
    项目中遇到一个问题服务器运行在tomcat中,有一个字符串需要加密输出来,包含中文,环境在本机用eclipse做的时候没有任何问题,可是一放到服务器上因为服务......
  • SpringBoot 第二部分知识点
    SpringBoot-第⼆天1.学习目标2Mybatis整合数据访问使用SpringBoot开发企业项目时,持久层数据访问是前端页面数据展示的基础,SpringBoot支持市面上常见的关系库......
  • SpringBoot 第一部分知识点
    SpringBoot-第⼀天1.学习目标 2.Spring框架发展史2.1.Spring1.x时代在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分放到不......
  • 红米手机如何修改安卓目录下的data目录下的txt文件
    两个应用:wpsoffice安卓清理君先用wps编辑保存一个txt的同名文件,当然wps并不能直接保存到安卓目录下这时候使用安卓清理君,删除+移动,就可以达到修改的目的其实安卓自带......
  • 差分处理修改区间问题
    #题目链接Problem-C-Codeforces 转换为差分后操作一:a1--a(i+1)++操作二:ai--操作三;a1++转换为差分数组后就可以转换问题为是的出了差分数组第一个元素外都......
  • 第六天(SpringBoot基础篇第三篇)
    一、Thymeleaf模板1.介绍Thymeleaf是用来开发Web和独立环境项目的现代服务器端Java模板引擎。Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板-HTML。......
  • 第六天(springBoot基础第三篇)
    一、Thymeleaf模板1.介绍Thymeleaf是用来开发Web和独立环境项目的现代服务器端Java模板引擎。Thymeleaf的主要目标是为您的开发工作流程带来优雅的自然模板-HTML。......
  • 添加删除修改(insert,update,【delete和truncate】)
                    ......
  • es5.1.1 修改索引数据
    #环境描述使用es版本为:5.1.1使用client为:TransportClientclient,不是highLevelClient#1.知道当前数据rowid时UpdateRequestupdateRequest=newUpdateRequest("index-2......