springboot动态修改properties文件,文件流的操作

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)

获取resources下的文件路径

String path = this.getClass().getClassLoader().getResource("jdbc.properties").getFile();
String path = this.getClass().getClassLoader().getResource("jdbc.properties").getPath();

当模块打包成jar后,我们获取到的路径可能会是下面这种,这种并不是规范的文件路径,因此通过 new File() 的方式访问时,会出现Not Found的错误

file:/C:/ResourceJar.jar!/resource/res.txt

通过访问流的方式对文件进行读操作,但是jar包中的文件不能进行写操作,这就是为什么需要文件服务器

InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");

对文件进行修改操作

String fileName = "d://test";
File file = new File(fileName);
//判断文件夹是否存在,不存在则创建
if(!file.exists()){
  file.mkdirs();  
}
File filePath = new File(fileName+"/test.txt");
//判断文件是否存在,不存在则创建
if(!filePath .exists()){
  filePath.createNewFile();  
}
//读取文件并按行将文件放入集合中
BufferedReader bufferedReader = null;
String str =null;
List<String> list = new ArrayList<>();
try {
    bufferedReader = new BufferedReader(new FileReader(filePath));
    while ((str = bufferedReader.readLine())!=null) {
       if(str.contains("username")) {
           list.add(str+"zhangsan");
           continue;
       }
       list.add(str);
    }
}catch(Exception e) {
      e.printStackTrace();
}finally {
  try {
        bufferedReader .close();
    }catch(IOException e) {
        e.printStackTrace();
    }  
}    
//遍历集合,按行写入文件
BufferedWriter bufferedWriter = null;
try {
    bufferedWriter = new BufferedWriter(new FileWriter(filePath));
    for (int i = 0; i < list.size(); i++) {
        if (i == list.size() -1) {
            bufferedWriter.write(list.get(i));
            bufferedWriter.flush();
        }else {
            bufferedWriter.write(list.get(i));
            bufferedWriter.newLine();
            bufferedWriter.flush();
        }
    }
}catch(Exception e) {
      e.printStackTrace();
}finally {
  try {
        bufferedWriter .close();
    }catch(IOException e) {
        e.printStackTrace();
    }  
}    

  

posted @ 2023-03-22 14:46  a快乐码农  阅读(790)  评论(0编辑  收藏  举报