This content has been marked as final. Show 3 replies
You can use a java.util.Properties object:
For example, you can have your properties stored in a file called configuration.txt, like this:
To load the properties you can do the following:
# This is the contents of the file configuration.txt # This is my first property. age = 18 # This is my second property name = richard # This is my third property buttontext = save
Then, to retrieve a property called "buttontext" you do this:
Properties properties = new Properties(); InputStream stream = new FileInputStream("C:/configuration.txt"); properties.load(stream); stream.close();
The getProperty method returns a String.
String value = properties.getProperty("buttontext");
Edited by: JoseLuis on Oct 5, 2008 11:36 AM
EDIT: Never mind, that was me not using the IDE properly. Thanks!
The above will only work once. For example if I grab the value of "buttontext" and run the program, the output would be "Save". However, if I then go and change the value of "buttontext" to "Load" the output remains the same.
Edited by: Joe544 on 05-Oct-2008 20:38
Indeed, that is the normal behavior if you read the property file once.
Normally in your application you have the configuration file. If you modify this file, and want the new values be read you must restart the application.
If you need that the program obtain the new values in the property file, you must read it again.
It may be easy to program, but it's a little inconvenient in terms of performance.
Another choice is to let the application manager to decide when to read the properties file. You can provide a button so the application manager can re-load the properties when the file has been modified.
You should decide the best approach for your needs:
1. Read the file once and restart the application if the values have ben changed.
2. Read the file each time a property is required (performance bottleneck).
3. Create a method to read the file and call it when you have modified the properties.
Edited by: JoseLuis on Oct 5, 2008 12:45 PM