springboot动态修改properties文件,文件流的操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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下的文件路径
1 2 | 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包中的文件不能进行写操作,这就是为什么需要文件服务器
1 | InputStream in = this .getClass().getClassLoader().getResourceAsStream( "jdbc.properties" ); |
对文件进行修改操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 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(); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· DeepSeek “源神”启动!「GitHub 热点速览」
· 上周热点回顾(2.17-2.23)