user195563
user195563

Reputation:

java update properties file run time

i am writing standalone java app for production monitoring. once it starts running the api is configured for default values which is set in .properties file. in running state the api's configuration can be changed and the .properties file should be updated accordingly. is there a way to achieve this ? or are there any other approaches to implement this ?

Thanks in advance

Upvotes: 17

Views: 42167

Answers (7)

Braj Thakur
Braj Thakur

Reputation: 41

Try this:

// Write in property file at runtime

public void setValue(String key, String value) {
    Properties props = new Properties();
     String path = directoryPath+ "/src/test/resources/runTime.properties";
    File f = new File(path);
    try {
        final FileInputStream configStream = new FileInputStream(f);
        props.load(configStream);
        configStream.close();
        props.setProperty(key, value);
        final FileOutputStream output = new FileOutputStream(f);
        props.store(output, "");
        output.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

// Read same file

 public String getValue(String key) {
        String value = null;
        try {
            Properties prop = new Properties();
            File f = new File(directoryPath+"/src/test/resources/runTime.properties");
            if (f.exists()) {
                prop.load(new FileInputStream(f));
                value = prop.getProperty(key);
            }
        } catch (Exception e) {
            System.out.println("Failed to read from runTime.properties");
        }
        return value;
    }

Upvotes: 0

David Pham
David Pham

Reputation: 1803

I completely agree that Apache Commons Configuration API is really good choice.

This example update properties at runtime

File propertiesFile = new File(getClass().getClassLoader().getResource(fileName).getFile());        
PropertiesConfiguration config = new PropertiesConfiguration(propertiesFile);           
config.setProperty("hibernate.show_sql", "true");
config.save();

From the post how to update properties file in Java

Hope this help!

Upvotes: 6

Pascal Thivent
Pascal Thivent

Reputation: 570595

You could use a very simple approach based on the java.util.Properties class which has indeed a load and store methods that you can use in conjunction with a FileInputStream and FileOutputStream:

But actually, I'd recommend to use an existing configuration library like Commons Configuration (amongst others). Check the Properties Howto to see how to load, save and automatically reload a properties file using its API.

Upvotes: 11

Sanju Thomas
Sanju Thomas

Reputation: 239

Apache common configuration API provided different strategies to reload property files at run time. FileChangedReloadingStrategy is one of them. Refer this link to see an example for property file reloading at run time using FileChangedReloadingStrategy.

Upvotes: 0

p3t0r
p3t0r

Reputation: 1980

java.util.Properties doesn't provide runtime reloading out-of-the-box as far as I know. Commons Configuration provides support for reloading configuration at runtime. The reload strategy can be configured by setting a ReloadingStrategy on the PropertiesConfiguration object. It also offers various other useful utilities for making your application configurable.

Upvotes: 3

Romain Linsolas
Romain Linsolas

Reputation: 81667

In addition to the load and store method of the Properties class, you can also use the Apache Commons Configuration library, which provides functions to easily manipulate configuration files (and not only .properties files).

Upvotes: 0

Steven Schlansker
Steven Schlansker

Reputation: 38536

The Java Properties class (api here) specifies "load" and "store" methods which should do exactly that. Use FileInputStream and FileOutputStream to specify the file to save it into.

Upvotes: 18

Related Questions