Java Reference
In-Depth Information
The Java Properties class helps you manage program properties. It allows you to
manage the properties either via external modification (someone editing a property file)
or internally by using the Properties.store() method.
The Properties object can be instantiated either without a file or with a pre-
loaded file. The files that the Properties object read are in the form of
[name]=[value] and are textually represented. If you need to store values in other
formats, you need to write to and read from a string.
If you are expecting the files to be modified outside the program (the user directly
opens a text editor and changes the values), be sure to sanitize the inputs; like trimming
the values for extra spaces and ignoring case if need be.
To query the different properties programmatically, you call the getProp-
erty(String) method, passing the string-based name of the property whose value
you want to retrieve. The method will return null if the property is not found. Alternat-
ively, you can invoke the getProperty (String,String) method, on which if
the property is not found in the Properties object, it will return the second para-
meter as its value. It is a good practice to specify default values in case the file doesn't
have an entry for a particular key.
Upon looking at a generated property file, you will notice that the first two lines in-
dicate the description of the file and the date when it was modified. These two lines
start with # , which in Java property files is the equivalent of a comment. The Prop-
erties object will skip any line starting with # when processing the file.
Note If you allow users to modify your configuration files directly, it is important to
have validation in place when retrieving properties from the Properties object. One
of the most common issues encountered in the value of properties is leading and/or trail-
ing spaces. If specifying a Boolean or integer property, be sure that they can be parsed
from a string. At a minimum, catch an exception when trying to parse to survive an un-
conventional value (and log the offending value).
8-13. Uncompressing Compressed Files
Problem
Your application has the requirement to decompress and extract files from a com-
pressed .zip file.
Search WWH ::




Custom Search