In Properties, we examined the way an application can use Properties objects to maintain its configuration. The Java platform itself uses a Properties object to maintain its own configuration. 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.
通过 Properties ,我们可以了解应用是如何使用 Properties 对象来维护其系统设置的。Java 平台本身使用Properties 对象来维护其自身配置。System 类维护着一
个 Properties 对象,该对象持有对当前工作环境的配置。系统属性包含了有关当前用户的,当前 java 运行时的,以及文件路径名分隔符等信息。
The following table describes some of the most important system properties 下表描述了一些重要的系统属性
Key 键
Meaning 含义
"file.separat
or"
Character that separates components of a file path. This is "/" on UNIX and "\" on Windows.
分隔文件路径组分的字符。UNIX 是 "/",Windows 是 "
\"。 "java.class.p
ath"
Path used to find directories and JAR archives containing class files. Elements of the class path are separated by a platform-specific character specified in the path.
separator property.
用于查找目录及包含类文件的JAR 文件的路径。类路径被 path.separator 属性中所指定的与平台相关的字符所分隔。 "java.home"
Installation directory for Java Runtime Environment (JRE) java
运行时环境(JRE)安装目录
"java.vendor" JRE vendor name JRE开发商 "java.vendor.
url"
JRE vendor URL JRE 开发商网址
"java.version
"
JRE version number JRE 版本号
"line.separat
or"
Sequence used by operating system to separate lines i
n text files
操作系统用来分隔文本文件中行的字符序列 "os.arch" Operating system architecture 操作系统架构 "os.name"
Operating system name 操作系统名车
"os.version" Operating system version 操作系统版本
"path.separator" Path separator character used in java.class.path 使用在 java.class.path 的路径分隔符 "user.dir" User working directory 用户工作目录 "user.home" User home directory 用户主目录 "user.name"
User account name 用户账户名称
Security consideration: Access to system properties can be restricted by the Security
Manager. This is most often an issue in applets, which are prevented from reading some system properties, and from writing any system properties. For more on accessing system properties in applets, refer to System Properties in the Doing More With Java Rich Internet Applications lesson.
相关安全性:系统属性的访问受限于 Security Manager 安全管理对象。这在 applets 中是最常遇到的问题,可以用来防止读一些系统属性及写入任何系统属性。更多关于在 appleets中访问系统属性,请参考与 System
Properties 相关课程 Doing More With Java Rich Internet Applications (Java富客户端互联网应用可以做更多)。
Reading System Properties 读系统属性
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.separator, use the following statement:
System 类有两个用来读取系统属性的方法:getProperty 和getProperties。
System 类有两个不同的版本的 getProperty 方法。都可以通过在参数列表中指定属性名称来获取相关属性值。两个 getProperty 的较为简单调用方法是使用单个参数,也就是属性的键。例如使用如下方法来获得 path.separator 属性的值。
System.getProperty("path.separator");
The getProperty method returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null.
getProperty 方法返回属性值的字符串。如果属性不存在,这一版本的 getProperty 返回 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.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!"
另一版本的 getProperty 方法需要两个字符串参数:第一个参数是用于查找的键,第二个阐述是如果建不存在或没有值时需要返回的默认值。例如下面的方法是查
找 System 属性 subliminal.message。该键值不是一个有效的系统属性,由于该方法通过第二的参数提供了缺省值,因此返回缺省值 "Buy StayPuft Marshmallows!" ,而不是返回空值 null。
System.getProperty("subliminal.message", "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.
System 类提供的最后一个方法 getProperties 方法用来访问属性值。它会返回一个 Properties 对象。该对象包含了整套系统属性的定义。
Writing System Properties 写入系统属性
To modify the existing set of system properties, use System.setProperties. 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.
使用 System.setProperties修改现有的系统属性设置。该方法通过初始化一个 Properties 对象用以包含属性值并用来对其进行设置。该方法用 Properties 对象新的一整套值来替换整套系统属性值。
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.
警告:更改系统属性有潜在的风险并请权衡利弊。许多系统属性在启动后不能反复读取并仅限于获取信息用途。更改一些属性可能会有无法预料的不良后果。
The next example, PropertiesTest, creates a Properties object and initializes it from myProperties.txt .
下一个例子, PropertiesTest,创建了一个 Properties 对象,并用 myProperties.txt 文件来对其初始化。
subliminal.message=Buy StayPuft Marshmallows!
PropertiesTest then uses System.setProperties to install the new Properties objects as the current set of system properties.
而后PropertiesTest 使用 System.setProperties 方法,利用新创建的 Properties 对象来完成对当前系统属性的设置。
import 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); } }
Note how PropertiesTest creates the Properties object, p, which is used as the argument to setProperties:
注意 PropertiesTest 是如何创建 Properties 对象 p的。 p 作为参数传递给方法 setProperties:
Properties p = new Properties(System.getProperties());
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.txt and sets the system properties to p. This has the effect of adding the properties listed in myProperties.txt to the set of properties created by the runtime system at startup. Note that an application can create p without any default Properties object, like this:
此语法初始化新的属性对象 p, p 具有与这个小应用相关的系统属性的当前设置。(系统属性值是被运行时系统初始化了的。)然后此应用又通过 myProperties.txt 文件给 p 加载了其他属性,并将 p 赋值给系统属性。这就会将列在文
件 myProperties.txt 当中的属性,添加到运行时系统所创建的属性设置中。注意,像这样,应用可以不依赖于任何 Properties 对象来创建 p 。
Properties p = new Properties();
Also note that the value of system properties can be overwritten! For example, if myProperties.txt contains the following line, the java.vendor system property will be overwritten: 还要值得注意的是系统属性值可以被覆写!如果 myProperties.txt 文件当中包含如下行,那么 java.vendor 系统属性将会被覆写。
java.vendor=Acme Software Company
In general, be careful not to overwrite system properties.
The setProperties method 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. 总之,要注意不要覆写系统属性。
setProperties 方法会改写当前所运行的应用的系统属性设置,但这些改写不是持久的。也就是说,在一个应用中的系统属性的变化不会影响到Java 解释器对这一应用及其他应用之后的调用。运行时系统在每次启动时会重新初始化系统属性。如果想要持久
的改变系统属性,那就需要应用将这些值在文件当中不存在时写入其中,并在启动时将其再次读出。
转载自:百度文库http://wenku.baidu.com/link?url=1mUXI5bOw3SSImbCE7h_k45FKd-4NMcNG2NdVb4Cnp-zIERIUrXNkyzOxbxUp2mPRSul6bq0BzV1R2GuyzB5tu0z3xQt-AmGXfdmeOa-aFO
其实就是一句话在自己的IDE(编译器)运行一下:
Properties properties=new Properties(System.getProperties());
properties.list(System.out);
自己打印出来看看有哪些属性就是了。