In Properties, we examined the way an application can usePropertiesobjects to maintain its configuration. The Java platform itself uses aPropertiesobject to maintain its own configuration. TheSystemclass maintains aPropertiesobject that describes the configuration of the current working environment. System properties include information about the current user, the current version of the Java runtime, and the character used to separate components of a file path name.The following table describes some of the most important system properties
Key Meaning "file.separator"Character that separates components of a file path. This is " /" on UNIX and "\" on Windows.
Reading System Properties
TheWriting System PropertiesSystemclass has two methods used to read system properties:getPropertyandgetProperties.TheSystemclass has two different versions ofgetProperty. Both retrieve the value of the property named in the argument list. The simpler of the twogetPropertymethods takes a single argument, a property key For example, to get the value ofpath.separator, use the following statement:
TheSystem.getProperty("path.separator");getPropertymethod returns a string containing the value of the property. If the property does not exist, this version ofgetPropertyreturns null.The other version ofgetPropertyrequires twoStringarguments: the first argument is the key to look up and the second argument is a default value to return if the key cannot be found or if it has no value. For example, the following invocation ofgetPropertylooks up theSystemproperty calledsubliminal.message. This is not a valid system property, so instead of returning null, this method returns the default value provided as a second argument: "Buy StayPuft Marshmallows!"
The last method provided by theSystem.getProperty("subliminal.message", "Buy StayPuft Marshmallows!");Systemclass to access property values is thegetPropertiesmethod, which returns aPropertiesobject. This object contains a complete set of system property definitions.
To modify the existing set of system properties, useSystem.setProperties. This method takes aPropertiesobject that has been initialized to contain the properties to be set. This method replaces the entire set of system properties with the new set represented by thePropertiesobject.
The next example,
Warning: Changing system properties is potentially dangerous and should be done with discretion. Many system properties are not reread after start-up and are there for informational purposes. Changing some properties may have unexpected side-effects.
, creates aPropertiesTestPropertiesobject and initializes it frommyProperties.txt.
subliminal.message=Buy StayPuft Marshmallows!PropertiesTestthen usesSystem.setPropertiesto install the newPropertiesobjects as the current set of system properties.
Note howimport java.io.FileInputStream; import java.util.Properties; public class PropertiesTest { public static void main(String[] args) throws Exception { // set up new properties object // from file "myProperties.txt" FileInputStream propFile = new FileInputStream( "myProperties.txt"); Properties p = new Properties(System.getProperties()); p.load(propFile); // set the system properties System.setProperties(p); // display new properties System.getProperties().list(System.out); } }PropertiesTestcreates thePropertiesobject,p, which is used as the argument tosetProperties:
This statement initializes the new properties object,Properties p = new Properties(System.getProperties());p, with the current set of system properties, which in the case of this small application, is the set of properties initialized by the runtime system. Then the application loads additional properties intopfrom the filemyProperties.txtand sets the system properties top. This has the effect of adding the properties listed inmyProperties.txtto the set of properties created by the runtime system at startup. Note that an application can createpwithout any defaultPropertiesobject, like this:
Also note that the value of system properties can be overwritten! For example, ifProperties p = new Properties();myProperties.txtcontains the following line, thejava.vendorsystem property will be overwritten:
In general, be careful not to overwrite system properties.Thejava.vendor=Acme Software CompanysetPropertiesmethod changes the set of system properties for the current running application. These changes are not persistent. That is, changing the system properties within an application will not affect future invocations of the Java interpreter for this or any other application. The runtime system re-initializes the system properties each time its starts up. If changes to system properties are to be persistent, then the application must write the values to some file before exiting and read them in again upon startup.
urs ramanujadasu
No comments:
Post a Comment
I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.