Amira
Amira

Reputation: 3280

updating a properties file in a web application

I have a properties file (under rsources folder) in which I'm stocking a variable (key=value),

I need to update it when a user insert a new value or update the older one , so can I do it? I have doubts because it's a web application so it' simply a war deployed in the server. So how it is possible to access the .properties file and change it directly from the code?

If it's not possible, is there another solution?

Upvotes: 0

Views: 1696

Answers (3)

steelshark
steelshark

Reputation: 644

You can let the user write to a properties file, but I don't think it's very clean to do. There is a class called "Properties" in the java.util package, you can use this class to load a representation of a physical properties file from your webapplication.

for example to load a properties file you could use following code:

 public void loadProps(File pfile) throws IOException {
    Properties props = new Properties();
    FileInputStream fis = new FileInputStream(propsFile);
    props.load(fis);    
    fis.close();
}

Now you can just use built in commands to manipulate the file: -setProperty(String key, String value); -get(Object key);

After you're done with it you can just call the save method on the properties Object. You will need an OutputStream for that.

Upvotes: 0

Sajan Chandran
Sajan Chandran

Reputation: 11487

Instead of modifying a properties file, you can create a new table in your database (e.g T_PROPERTIES) and add/modify rows in the table. Define the table with 2 column, key and value and change the records accordingly.

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272417

Perhaps the user could provide an overriding properties file in the filesystem, whose values would override the packaged default properties file.

Check out Apache Commons Configuration, which permits this capability.

Often you want to provide a base set of configuration values, but allow the user to easily override them for their specific environment. Well one way is to hard code the default values into your code, and have then provide a property file that overrides this. However, this is a very rigid way of doing things. Instead, with the CompositeConfiguration you can provide many different ways of setting up a configuration.

Upvotes: 4

Related Questions