简体   繁体   English

从Java附加属性文件

[英]append properties file from java

From java I want to read .properties file and if property is present I want to re-set the property again. 从Java我想读取.properties文件,如果存在property,我想再次重新设置该property。 lets say .properties file has entry password=123 now I want to check If password entry is there replace 123 with 567. but need to keep all content as it is. 假设.properties文件的条目密码为123,现在我要检查是否存在密码条目,将123替换为567。但是需要保留所有内容。 how to do that? 怎么做? please help 请帮忙

One solution I can think of rightaway is 我能想到的一种解决方案是

  1. Load all the properties from the base .properties file. 从基本.properties文件加载所有属性。
  2. Create a new temp properties file and loop through the entries from original file to write the same in to new file. 创建一个新的临时属性文件,并循环访问原始文件中的条目,以将其写入新文件。 In the loop you can change the value of any property while writing it to a new file. 在循环中,您可以在将任何属性的值写入新文件时更改其值。
  3. After the loop delete the original file and rename the temp file as original file. 循环后,删除原始文件,然后将临时文件重命名为原始文件。

This approach has couple of limitations, 这种方法有几个局限性,

  1. Do not use it if you are really concerned about the file last modified/created date, since we are creating a whole new file here. 如果您确实担心文件的上次修改/创建日期,请不要使用它,因为我们在这里创建一个全新的文件。
  2. If the original file is too big, this approach may cause memory problems. 如果原始文件太大,此方法可能会导致内存问题。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM