Where is systems properties




















Which brings us to the next version of getProperty method. This version requires two String arguments: 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, this call to getProperty looks up the System property called subliminal. This is not a valid system property, so instead of returning null, this method returns the default value provided as a second argument: " Buy Java Now! You can use the various Properties class methods to query the Properties objects for specific values or to list the entire set of properties.

Writing System Properties You can modify the existing set of system properties using System 's setProperties method. Thus this method replaces the entire set of system properties with the new set represented by the Properties object. The next example is a Java program that creates a Properties object and initializes it from this file: myProperties.

The example program then uses System. FileInputStream; import java. Then the program loads additional properties into p from the file myProperties. This has the effect of adding the properties listed in myProperties. Also note that the value of system properties can be overwritten! For example, if myProperties. The setProperties method changes the set of system properties for the current running application.

These changes are not persistent. The System class maintains a Properties object 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 System class has two methods used to read system properties: getProperty and getProperties. The System class has two different versions of getProperty.

Both retrieve the value of the property named in the argument list. The simpler of the two getProperty methods takes a single argument, a property key For example, to get the value of path. The getProperty method returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null. The other version of getProperty requires two String arguments: 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 of getProperty looks up the System property called subliminal. 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 the System class to access property values is the getProperties method, which returns a Properties object. This object contains a complete set of system property definitions.

To modify the existing set of system properties, use System. This method takes a Properties object 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 the Properties object. The next example, PropertiesTest , creates a Properties object and initializes it from myProperties.

PropertiesTest then uses System. Note how PropertiesTest creates the Properties object, p , which is used as the argument to setProperties :. This statement initializes the new properties object, 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 into p from the file myProperties. This has the effect of adding the properties listed in myProperties.



0コメント

  • 1000 / 1000