代码之家  ›  专栏  ›  技术社区  ›  Miserable Variable

更新属性文件的更好类?

  •  34
  • Miserable Variable  · 技术社区  · 16 年前

    虽然 java.util.properties 允许读取和写入属性文件,写入不保留格式。不足为奇,因为它没有绑定到属性文件。

    有没有 PropertyFile 在那里——或者类似的——保存注释和空行并在适当的位置更新属性值的类?

    7 回复  |  直到 6 年前
        1
  •  48
  •   iamkenos    8 年前

    它不会比阿帕奇的公地好多少。 Configuration 应用程序编程接口。这提供了从属性文件、XML、JNDI、JDBC数据源等进行配置的统一方法。

    它对财产档案的处理非常好。它允许您生成 PropertiesConfigurationLayout 对象,它将尽可能多地保留有关属性文件的信息(空白、注释等)。保存对属性文件的更改时,这些更改将尽可能地保留。


    样例代码:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStreamReader;
    
    import org.apache.commons.configuration.ConfigurationException;
    import org.apache.commons.configuration.PropertiesConfiguration;
    import org.apache.commons.configuration.PropertiesConfigurationLayout;
    
    public class PropertiesReader {
        public static void main(String args[]) throws ConfigurationException, FileNotFoundException {
            File file = new File(args[0] + ".properties");
    
            PropertiesConfiguration config = new PropertiesConfiguration();
            PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);
            layout.load(new InputStreamReader(new FileInputStream(file)));
    
            config.setProperty("test", "testValue");
            layout.save(new FileWriter("path\\to\\properties\\file.properties", false));
        }
    }
    

    参见:

        2
  •  7
  •   John Rix    9 年前

    使用PatrickBoos提供的ApacheCommons配置库的示例代码是不必要的复杂。除非需要对输出进行一些高级控制,否则不需要显式使用propertiesconfigurationlayout。属性配置本身足以保留注释和格式:

    PropertiesConfiguration config = new PropertiesConfiguration("myprops.properties");
    config.setProperty("Foo", "Bar");
    config.save();
    

    (注:此代码适用于现有的1.10稳定版本。我没有检查它是否在当前可用的2.0 alpha版本上工作。)

        3
  •  6
  •   Romain Linsolas    16 年前

    你可以看看 Apache Commons Configuration ,包含 PropertiesConfiguration 班级。 但是,由于我从未使用过它,我不知道它是否保留了注释和格式…

    不过,这值得一试…

        4
  •  1
  •   Tarun Gupta    8 年前
        File file = new File("src/test/resources/1automation.properties");
        PropertiesConfiguration config = new PropertiesConfiguration();
        PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);
        layout.load(new InputStreamReader(new FileInputStream(file)));
        FileWriter fw = new FileWriter("src/test/resources/1automation.properties",false);
        config.setProperty("myssi.admin.name", "testValue");
        layout.save(fw);
    
        5
  •  0
  •   Kevin Le    6 年前

    configuration2类具有不同的语法。下面是一个使用它们的示例:

    import org.apache.commons.configuration2.PropertiesConfiguration;
    import org.apache.commons.configuration2.PropertiesConfigurationLayout;
    
    public void test() {
        PropertiesConfiguration config = new PropertiesConfiguration();
        PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout();
        config.setLayout(layout);
        layout.load(config, new FileReader("config.properties"));
    
        config.setProperty("KEY", "VALUE");
        StringWriter stringWriter = new StringWriter();
        layout.save(config, stringWriter);
        LOG.debug("Properties:\n{}", stringWriter.toString());
    }
    
        6
  •  0
  •   Jeremy    6 年前

    最佳答案包含一个小错误: 线:

    PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);
    

    必须替换为:

    PropertiesConfigurationLayout layout = config.getLayout();
    
        7
  •  -1
  •   Aaron Digulla    16 年前

    我曾经看到一个类用ini文件来完成这个操作,但现在找不到链接了。如果你找不到其他东西,你可以试试 DecentXML . 我用特定的设计目标编写了这个XML解析器,以100%保留原始格式(即,在元素中或根元素周围使用注释、奇怪的空格,等等)。

    在解析生成的XML文档期间,您只需记住包含选项值的元素,并替换其中的文本节点。当你存钱时,任何未动的东西都不会以任何方式改变。