如何在不覆盖整个文件的情况下覆盖.properties中的一个属性?

基本上,我必须通过Java应用程序覆盖.properties文件中的某个属性,但是当我使用Properties.setProperty()和Properties.Store()时,它会覆盖整个文件而不是仅覆盖那个属性。

我尝试使用append = true构建FileOutputStream,但是它添加了另一个属性,并且不会删除/覆盖现有属性。

我如何对其进行编码,以便设置一个属性会覆盖该特定属性,而不会覆盖整个文件?

编辑:我尝试读取文件并添加到它。 这是我更新的代码:

FileOutputStream out = new FileOutputStream("file.properties"); FileInputStream in = new FileInputStream("file.properties"); Properties props = new Properties(); props.load(in); in.close(); props.setProperty("somekey", "somevalue"); props.store(out, null); out.close(); 

Properties API不提供在属性文件中添加/替换/删除属性的任何方法。 API支持的模型是从文件加载所有属性,更改内存中的Properties对象,然后将所有属性存储到文件(相同的一个或不同的文件)。

但是Properties API在这方面并不罕见。 实际上,如果不重写整个文件,就很难实现文本文件的就地更新。 这种困难是现代操作系统实现文件/文件系统方式的直接结果。

如果确实需要进行增量更新,则需要使用某种数据库来保存属性,而不是“.properties”文件。

您可以使用Apache Commons Configuration中的 PropertiesConfiguration。

在版本1.X中:

 PropertiesConfiguration config = new PropertiesConfiguration("file.properties"); config.setProperty("somekey", "somevalue"); config.save(); 

从2.0版开始:

 Parameters params = new Parameters(); FileBasedConfigurationBuilder builder = new FileBasedConfigurationBuilder(PropertiesConfiguration.class) .configure(params.properties() .setFileName("file.properties")); Configuration config = builder.getConfiguration(); config.setProperty("somekey", "somevalue"); builder.save(); 

另一个答案让我想起了Apache Commons配置库,特别是PropertiesConfigurationLayoutfunction。

这允许(或多或少)保留原始布局,注释,排序等。

属性文件是为应用程序提供配置的简单方法,但不一定是进行程序化,用户特定自定义的好方法,原因就在于您找到的原因。

为此,我使用了Preferences API。

我做了以下方法: –

  1. 读取文件并加载属性对象
  2. 使用“.setProperty”方法更新或添加新属性。 (setProperty方法优于.put方法,因为它可以用于插入以及更新属性对象)
  3. 将属性对象写回文件以使文件与更改保持同步。
 import java.io.*; import java.util.*; class WritePropertiesFile { public static void main(String[] args) { try { Properties p = new Properties(); p.setProperty("1", "one"); p.setProperty("2", "two"); p.setProperty("3", "three"); File file = new File("task.properties"); FileOutputStream fOut = new FileOutputStream(file); p.store(fOut, "Favorite Things"); fOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 
 public class PropertiesXMLExample { public static void main(String[] args) throws IOException { // get properties object Properties props = new Properties(); // get path of the file that you want String filepath = System.getProperty("user.home") + System.getProperty("file.separator") +"email-configuration.xml"; // get file object File file = new File(filepath); // check whether the file exists if (file.exists()) { // get inpustream of the file InputStream is = new FileInputStream(filepath); // load the xml file into properties format props.loadFromXML(is); // store all the property keys in a set Set names = props.stringPropertyNames(); // iterate over all the property names for (Iterator i = names.iterator(); i.hasNext();) { // store each propertyname that you get String propname = i.next(); // set all the properties (since these properties are not automatically stored when you update the file). All these properties will be rewritten. You also set some new value for the property names that you read props.setProperty(propname, props.getProperty(propname)); } // add some new properties to the props object props.setProperty("email.support", "donot-spam-me@nospam.com"); props.setProperty("email.support_2", "donot-spam-me@nospam.com"); // get outputstream object to for storing the properties into the same xml file that you read OutputStream os = new FileOutputStream( System.getProperty("user.home") + "/email-configuration.xml"); // store the properties detail into a pre-defined XML file props.storeToXML(os, "Support Email", "UTF-8"); // an earlier stored property String email = props.getProperty("email.support_1"); System.out.println(email); } } } 

该计划的输出将是:

 support@stackoverflow.com 

对的,这是可能的。 如果您不想从属性文件中删除您的内容。 只需读取并替换文件中的字符串即可。

  String file="D:\\path of your file\abc.properties"; Path path = Paths.get(file); Charset charset = StandardCharsets.UTF_8; String content = new String(Files.readAllBytes(path), charset); content = content.replaceAll("name=anything", "name=anything1"); Files.write(path, content.getBytes(charset)); 

上述代码不会删除文件中的内容。 它只是替换文件中的部分内容。