它并没有比Apache的Commons
Configuration API
更好。这提供了从属性文件,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)); }}
也可以看看:
- http://mvnrepository.com/artifact/commons-configuration/commons-configuration/
- https://commons.apache.org/proper/commons-configuration/apidocs/org/apache/commons/configuration2/PropertiesConfigurationLayout.html
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)