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 loading. Note that this method does not * do any synchronization. This lies in the responsibility of the caller. * (Typically, the caller is a {@code FileHandler} object which takes * care for proper synchronization.) * * @since 2.0 */ @Override public void read(final Reader in) throws ConfigurationException, IOException { getLayout().load(this, in); }
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);
private PropertiesConfiguration loadPropertiesFile(File file, PropertiesConfiguration config) { PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); try (InputStreamReader is = new InputStreamReader(new FileInputStream(file))) { layout.load(config, is); } catch (ConfigurationException | FileNotFoundException e) { LOGGER.log(Level.WARNING, "Failed to load " + file.getPath(), e); } catch (IOException e) { e.printStackTrace(); } config.setLayout(layout); return config; } }
public String getValue(String property) { String propertyValue = EMPTY; PropertiesConfiguration config = new PropertiesConfiguration(); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); try (InputStream input = new FileInputStream(props)) { layout.load(config, new InputStreamReader(input, UTF_8)); propertyValue = config.getString(property); } catch (Exception e) { LOGGER.error(e.getMessage()); } return propertyValue; }
public Map<String, String> getKeysAndValues() { Map<String, String> kvp = newLinkedHashMap(); PropertiesConfiguration config = new PropertiesConfiguration(); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); try (InputStream input = new FileInputStream(props)) { layout.load(config, new InputStreamReader(input, UTF_8)); config.getKeys().forEachRemaining(item -> kvp.put(item, config.getString(item))); } catch (Exception e) { LOGGER.error(e.getMessage()); } return kvp; }
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()); } } }