0

I am developing a JSF Web Application, which is a control panel to change 8 Parameters. As I don't want to create a database for only 8 params I wanted to store them in a properties file. I am already able to read them from my properties file but i fail writing them back. Here is my code.

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.sun.javafx.fxml.PropertyNotFoundException;

public class PropertyAccess {

private static final String DEFAULT_PROP_FILE = "params.properties";
private String propFileName;
private Properties props;

public PropertyAccess() {
    this.propFileName = DEFAULT_PROP_FILE;
}

public PropertyAccess(String propFileName) {
    this.propFileName = propFileName;
}

public String getProperty(String propertyKey) {

    props = new Properties();
    InputStream in = getClass().getClassLoader().getResourceAsStream(propFileName);

    try {

        if (in != null) {
            props.load(in);
            in.close();
        } else {
            throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    String result = props.getProperty(propertyKey);
    if (result==null){
        throw new PropertyNotFoundException("Property '" + propertyKey + "' not found in '" + propFileName + "'");
    }


    return result;
}

public void setProperty(String propertyKey, String propertyValue){
    try {
        props = new Properties();
        props.setProperty(propertyKey, propertyValue);

        FileOutputStream out = new FileOutputStream(propFileName);
        props.store(out, null);
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

That is my property access class. But writing the properties does not work:

PropertyAccess pa = new PropertyAccess();
pa.setProperty("test", "testvalue");
pa.getProperty("test");

throws an Exception that the property wasnt found.

Any Ideas?

buntspecht
  • 16
  • 3
  • Even if this would work, you are aware you're overwriting the complete file one property at a time, right? – mabi Jun 30 '15 at 16:40
  • Okay, so, is there a better solution to just overwrite a single property? – buntspecht Jun 30 '15 at 17:25
  • Yes. The answer depends on what you do. Jsf Web app means you'll be using some kind of application server? Consider designating a file outside of your webapp (see http://stackoverflow.com/a/19142316/785663 for a similar problem) and write to it. To fix your single property problem, initialize the `Properties` in your constructor and act on the field instead of reading and writing it each time. – mabi Jun 30 '15 at 17:58
  • Yes, search stackoverflow for a java config framework. I did it for you: http://stackoverflow.com/questions/2407765/java-which-configuration-framework-to-use – Kukeltje Jun 30 '15 at 17:58

0 Answers0