Java Read Build Properties at Run Time
Properties are configuration values managed as central/value pairs. In each pair, the key and value are both String
values. The key identifies, and is used to call up, the value, much equally a variable name is used to think the variable's value. For instance, an application capable of downloading files might use a belongings named "download.lastDirectory" to go along track of the directory used for the final download.
To manage properties, create instances of java.util.Backdrop
. This grade provides methods for the following:
- loading key/value pairs into a
Properties
object from a stream, - retrieving a value from its key,
- list the keys and their values,
- enumerating over the keys, and
- saving the backdrop to a stream.
For an introduction to streams, refer to the section I/O Streams in the Bones I/O lesson.
Properties
extends coffee.util.Hashtable
. Some of the methods inherited from Hashtable
support the following actions:
- testing to see if a item primal or value is in the
Backdrop
object, - getting the electric current number of fundamental/value pairs,
- removing a key and its value,
- adding a cardinal/value pair to the
Properties
listing, - enumerating over the values or the keys,
- retrieving a value by its central, and
- finding out if the
Properties
object is empty.
Security Considerations: Access to backdrop is subject to approval by the electric current security manager. The instance code segments in this department are assumed to be in standalone applications, which, past default, accept no security director. The same code in an applet may not work depending on the browser in which it is running. Come across What Applets Can and Cannot Do in the Coffee Applets lesson for information nigh security restrictions on applets.
The System
class maintains a Properties
object that defines the configuration of the electric current working environment. For more about these properties, see Organisation Properties. The residue of this section explains how to utilise backdrop to manage application configuration.
Properties in the Application Life Cycle
The following figure illustrates how a typical awarding might manage its configuration data with a Properties
object over the class of its execution.
-
Starting Up
The actions given in the first three boxes occur when the application is starting upwards. First, the awarding loads the default backdrop from a well-known location into aProperties
object. Usually, the default backdrop are stored in a file on deejay along with the.class
and other resource files for the application.
Next, the application creates anotherProperties
object and loads the properties that were saved from the last fourth dimension the application was run. Many applications store properties on a per-user basis, so the properties loaded in this step are usually in a specific file in a detail directory maintained by this application in the user'due south home directory. Finally, the awarding uses the default and remembered properties to initialize itself.
The cardinal here is consistency. The awarding must e'er load and save properties to the same location then that it tin find them the next time it's executed.
-
Running
During the execution of the awarding, the user may change some settings, perhaps in a Preferences window, and theProperties
object is updated to reflect these changes. If the users changes are to be remembered in future sessions, they must be saved. -
Exiting
Upon exiting, the application saves the backdrop to its well-known location, to exist loaded once more when the application is next started upwards.
Setting Upward the Properties Object
The following Coffee lawmaking performs the first two steps described in the previous section: loading the default properties and loading the remembered properties:
. . . // create and load default properties Properties defaultProps = new Properties(); FileInputStream in = new FileInputStream("defaultProperties"); defaultProps.load(in); in.shut(); // create application properties with default Backdrop applicationProps = new Properties(defaultProps); // now load backdrop // from last invocation in = new FileInputStream("appProperties"); applicationProps.load(in); in.shut(); . . .
Beginning, the application sets up a default Properties
object. This object contains the set of properties to use if values are not explicitly set elsewhere. Then the load method reads the default values from a file on disk named defaultProperties
.
Next, the application uses a dissimilar constructor to create a 2nd Properties
object, applicationProps
, whose default values are independent in defaultProps
. The defaults come into play when a belongings is being retrieved. If the property can't exist constitute in applicationProps
, then its default list is searched.
Finally, the code loads a set of properties into applicationProps
from a file named appProperties
. The properties in this file are those that were saved from the application the terminal time it was invoked, equally explained in the adjacent section.
Saving Properties
The post-obit example writes out the application properties from the previous example using Backdrop.shop
. The default properties don't need to exist saved each time because they never alter.
FileOutputStream out = new FileOutputStream("appProperties"); applicationProps.store(out, "---No Comment---"); out.close();
The store
method needs a stream to write to, as well every bit a string that it uses equally a comment at the top of the output.
Getting Property Information
In one case the application has set its Properties
object, the application can query the object for information most diverse keys and values that information technology contains. An application gets information from a Properties
object afterward start up so that it tin can initialize itself based on choices fabricated by the user. The Properties
class has several methods for getting property data:
-
contains(Object value)
andcontainsKey(Object key)
Returnstrue
if the value or the primal is in theProperties
object.Backdrop
inherits these methods fromHashtable
. Thus they acceptObject
arguments, but onlyCord
values should be used. -
getProperty(String key)
andgetProperty(Cord fundamental, String default)
Returns the value for the specified property. The second version provides for a default value. If the central is not found, the default is returned. -
list(PrintStream s)
andlist(PrintWriter w)
Writes all of the properties to the specified stream or writer. This is useful for debugging. -
elements()
,keys()
, andpropertyNames()
Returns anEnumeration
containing the keys or values (as indicated by the method name) contained in theProperties
object. Thekeys
method only returns the keys for the object itself; thepropertyNames
method returns the keys for default properties also. -
stringPropertyNames()
LikepropertyNames
, but returns aReady<Cord>
, and only returns names of properties where both key and value are strings. Notation that theSet
object is not backed by theProperties
object, and so changes in i do not bear upon the other. -
size()
Returns the electric current number of cardinal/value pairs.
Setting Properties
A user's interaction with an application during its execution may impact property settings. These changes should be reflected in the Properties
object so that they are saved when the application exits (and calls the store
method). The post-obit methods change the properties in a Backdrop
object:
-
setProperty(String key, String value)
Puts the cardinal/value pair in theProperties
object. -
remove(Object fundamental)
Removes the key/value pair associated with key.
Note: Some of the methods described to a higher place are defined in Hashtable
, and thus accept key and value argument types other than String
. Always use String
s for keys and values, even if the method allows other types. Also do non invoke Hashtable.fix
or Hastable.setAll
on Properties
objects; always use Backdrop.setProperty
.
Java Read Build Properties at Run Time
Source: https://docs.oracle.com/javase/tutorial/essential/environment/properties.html
0 Response to "Java Read Build Properties at Run Time"
Post a Comment