Hi
When deploying large projects, you may need some way to set some
configuration data.
Old Windows programs used to use an ".ini" file containing configuration
data (and Windows used to provide APIs to read or write properties from
or into the application ".ini" file). Configuration file entries (items)
had the form "key=value" (it contained values associated to key names).
Nowadays Windows application rather use registry entries (a non portable
idea, giving access to an application to some sensible resources
containing not only the configuration data for the current application
but for all the installed application as well as to security
information), but the configuration ".ini" files are still used.
The class "java.util.Properties" contains just a hash table associating
values to keys, but it is specialized for managing configuration data.
You can get a "Properties" object calling "System.getProperties()". The
"Properties" object you get contains all the basic properties of your
Java session, the JVM configuration and some system settings. They are
important properties and the JVM gather them from various sources,
including system information (through the registry database on Windows
or system API calls on other systems), command-line parameters (such as
"-cp") or environment variables (such as CLASSPATH) and user-specific
parameters (such as "-Dfoo=bar" that associates the value "bar" to the
key name "foo").
You can obtain the value associated to a key (such as "java.class.path"
for the current "classpath") using either
System.getProperties().getProperty("java.class.path") or ...
getProperty("java.class.path", ".") in order to specify the default
value "." (to be used if the requested value is undefined).
Have fun displaying all the current system properties using:
...
public static void main(String[] args) {
java.util.Properties props = System.getProperties();
java.util.Enumeration names = props.propertyNames();
while (names.hasMoreElements()){
String name = (String)names.nextElement();
String value = props.getProperty(name);
System.out.println(name + "=" + value);
}
}
...
The Properties object also allows to add a new property (or to change
the value of an existing one) using "setProperty(String key, String
value)", which is of not much use for real life applications for the
system properties.
But your application can use its own configuration file (that contains,
for e.g., the name of the JDBC driver class and the database connection
parameters - so that they will not be hardcoded into the program). You
can load the configuration from the file into a Properties object:
Properties props = new Properties();
props.load( someInputStreamConnectedToTheFile ); // This will need
some try/catch statement
Then read the values of some property using "props.getProperty(name)"
(or the variant using a default value).
The user might be allowed to change some configuration data (such as to
start with the maximized window or the normal size one). The new value
can be stored into the Properties object using something like
"props.setProperty("ini.magnified", "true")". The change is, of course,
useless unless the values in the Properties object are saved back to the
configuration file using something like "props.store( someOutputStream,
aCommentString )".
It is possible to do something even more clumsy:
-- define a Properties object containing the default values for the
properties
-- load the Properties from the configuration file using "load" and
combining with the default values
-- read the values (either the read ones or the default values will be
issued)
-- modify the values
-- write the modified values using "store" (that will not store the
default values)
In this way, your application can deal with some "standard default
values" (and avoid mistakes when calling "getProperty" for the same
property but from various segments of code - possible with different
default values explicitly specified).
Ok, the details for this operation may be too much for this step.
Input and Ouput streams (for reading from and writing to files) are
described in the chapter 9 of the JavaPassion course (the part
concerning the I/O streams), and the try/catch exception management is
described in the chapter 5 (the part concerning the Exceptions).
Hope it helps
mihai
Anjani Kumar Narnur a écrit :
Hi Guys,
Can someone shed more light on the Properties class ? I didnt quite
understand getProperties() and setProperties() methods and their use
in the real world code.
Thanks.
--
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to
this email with the words "REMOVE ME" as the subject.
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to this email with the
words "REMOVE ME" as the subject.