Java 如何编辑属性文件而不破坏其其余部分

Java 如何编辑属性文件而不破坏其其余部分,java,Java,示例.属性 user=somePerson env=linux file=mpg propertiestTest.java { Properties props = new Properties(); props.setProperty("user", "GodIsUser"); final File propsFile = new File(someDir/Example.properties"); props.store(new FileOu

示例.属性

user=somePerson
env=linux
file=mpg
propertiestTest.java
   {
     Properties props = new Properties();
     props.setProperty("user", "GodIsUser");
     final File propsFile = new File(someDir/Example.properties");

     props.store(new FileOutputStream(propsFile), "");
}
user=GodIsUser
properties.java类

user=somePerson
env=linux
file=mpg
propertiestTest.java
   {
     Properties props = new Properties();
     props.setProperty("user", "GodIsUser");
     final File propsFile = new File(someDir/Example.properties");

     props.store(new FileOutputStream(propsFile), "");
}
user=GodIsUser
重新生成示例属性的结果

user=somePerson
env=linux
file=mpg
propertiestTest.java
   {
     Properties props = new Properties();
     props.setProperty("user", "GodIsUser");
     final File propsFile = new File(someDir/Example.properties");

     props.store(new FileOutputStream(propsFile), "");
}
user=GodIsUser

所有其他条目都会被删除

您应该先使用
props.load(inStream)
加载现有的道具。

首次使用,仅在使用后使用:通过使用
properties.setProperty()

修改属性您应该使用
props.load(inStream)
首先加载现有的道具。

首次使用,仅在使用后使用:使用
properties.setProperty()
修改属性您需要首先使用以下命令从文件中填充属性:


您需要首先使用以下命令从文件中填充它:

  • 将属性文件读入属性对象
  • 添加新属性
  • 存储新更新的属性对象
  • 上面的第一步是关键

  • 将属性文件读入属性对象
  • 添加新属性
  • 存储新更新的属性对象

  • 上面的步骤1是关键。

    您可以使用
    put

    //Load the props
    final File propsFile = new File(someDir/Example.properties");
    
    Properties props = load(new FileOutputStream(propsFile));
    props.put("user", "GodIsUser");
    
    props.store(new FileOutputStream(propsFile), "");
    

    您可以使用
    put

    //Load the props
    final File propsFile = new File(someDir/Example.properties");
    
    Properties props = load(new FileOutputStream(propsFile));
    props.put("user", "GodIsUser");
    
    props.store(new FileOutputStream(propsFile), "");
    
    如果需要关闭该文件,请按上述操作


    如果需要关闭文件,请按上述操作。

    谢谢!响应时间太棒了!非常感谢。响应时间太棒了!