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(propsFile.newWriter()); } }
@Override public void scrubRegistrationProperties() { if (!exist()) { return; } try { PropertiesConfiguration config = new PropertiesConfiguration(); config.setIOFactory(new FilteringOutputWriterFactory()); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); layout.setLineSeparator("\n"); layout.load(config, reader()); try (FileWriter out = new FileWriter(this.configFile)) { layout.save(config, out); } loadProperties(); } catch (ConfigurationException | IOException e) { LOG.warn("[Agent Auto Registration] Unable to scrub registration key.", e); } }
/** * {@inheritDoc} This implementation delegates to the associated layout * object which does the actual saving. Note that, analogous to * {@link #read(Reader)}, this method does not do any synchronization. * * @since 2.0 */ @Override public void write(final Writer out) throws ConfigurationException, IOException { getLayout().save(this, out); }
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);
public void setValue(String property, String propertyValue) { PropertiesConfiguration config = new PropertiesConfiguration(); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); try (InputStream input = new FileInputStream(props)) { layout.load(config, new InputStreamReader(input, UTF_8)); config.setProperty(property, propertyValue); layout.save(config, new FileWriter(props, false)); } catch (Exception e) { LOGGER.error(e.getMessage()); } } }
CombinedConfiguration props = new CombinedConfiguration(); final PropertiesConfiguration defaultsProps = new PropertiesConfiguration(new File("/tmp/default.properties")); final PropertiesConfiguration customProps = new PropertiesConfiguration(new File("/tmp/custom.properties")); props.setNodeCombiner(new OverrideCombiner()); props.addConfiguration(customProps); //first should be loaded the override values props.addConfiguration(defaultsProps); // last your 'default' values PropertiesConfiguration finalFile = new PropertiesConfiguration(); finalFile.append(props); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(finalFile, defaultsProps.getLayout()); //here we copy the layout from the 'base file' layout.save(new FileWriter(new File("/tmp/app.properties")));