Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com

The gist.

All kinds of settings, user preferences, default settings, program parameters and the likes, can easily be stored in a plain-text file by use of the java.util.Properties class.

The file we use here contains this :

two comment lines ....

and three key-value pairs .

When the load button is clicked, a URL is created for the properties file that's in the same directory on our site as the Propslet.class file .

An input stream is opened on it .

In a stand-alone application you would do something along these lines.

Next, a java.util.Properties instance is created ....

and the file's contents stored in it :

Then the Properties list is asked for an Iterator reference, which makes it easy to work through the entire list's keys :

If, and as long as, there are keys in the list, we ask for the next key ....

and use it to retrieve the associated value ,....

which is then shown in the java.awt.TextArea .

Selecting a specific property is done like this :

Next, the load button is disabled and the save button enabled :

The actionPerformed method is left .

When the Save button is clicked, we check if something has been entered in the TextField and use it to set the Volume property :

And the entire properties list would be saved in a local file if your JVM would permit it, but it won't, it will throw a SecurityException.

OK, so this example isn't consistent, but we just want to give you an idea of calling properties store method.

To handle this properly, you would need something like a servlet on the server, that would receive the data and store it.

You just tell the Properties object to save itself to a stream, whether it's a file-stream or a URL-stream.

==========

NOTES

==========

Setting a property that's not yet in the list, adds it to the list, otherwise it's overwritten.

==========

getProperty returns null if the property is unknown.

You can solve this like so, where 52 portrays a default value.

getProperty always return a String, even Generics won't help, you'll have to code the conversion, if needed.

==========

Since 1.5 we have these methods, that pretty much speak for themselves.

.