JAva Properties学習ノート
39015 ワード
Properties
まずコードを見てください.
まずコードを見てください.
public
class Properties extends Hashtable<Object,Object> {
/**
* use serialVersionUID from JDK 1.1.X for interoperability
*/
@java.io.Serial
private static final long serialVersionUID = 4112578634029874840L;
private static final Unsafe UNSAFE = Unsafe.getUnsafe();
/**
* A property list that contains default values for any keys not
* found in this property list.
*
* @serial
*/
protected volatile Properties defaults;
/**
* Properties does not store values in its inherited Hashtable, but instead
* in an internal ConcurrentHashMap. Synchronization is omitted from
* simple read operations. Writes and bulk operations remain synchronized,
* as in Hashtable.
*/
private transient volatile ConcurrentHashMap<Object, Object> map;
// ....
}
```
,Properties Hashtable. 。 :Properties defaults ConcurrentHashMap<Object, Object> map。
```java
/**
* Calls the {@code Hashtable} method {@code put}. Provided for
* parallelism with the {@code getProperty} method. Enforces use of
* strings for property keys and values. The value returned is the
* result of the {@code Hashtable} call to {@code put}.
*
* @param key the key to be placed into this property list.
* @param value the value corresponding to {@code key}.
* @return the previous value of the specified key in this property
* list, or {@code null} if it did not have one.
* @see #getProperty
* @since 1.2
*/
public synchronized Object setProperty(String key, String value) {
return put(key, value);
}
@Override
public synchronized Object put(Object key, Object value) {
return map.put(key, value);
}
/**
* Searches for the property with the specified key in this property list.
* If the key is not found in this property list, the default property list,
* and its defaults, recursively, are then checked. The method returns
* {@code null} if the property is not found.
*
* @param key the property key.
* @return the value in this property list with the specified key value.
* @see #setProperty
* @see #defaults
*/
public String getProperty(String key) {
Object oval = map.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
Properties defaults;
return ((sval == null) && ((defaults = this.defaults) != null)) ? defaults.getProperty(key) : sval;
}
```
, , map 。 ,Properties Hashtable. defaults , private transient volatile ConcurrentHashMap<Object, Object> map; 。
### System properties
```java
public static void main(String args[]) {
/**
* System properties. The following properties are guaranteed to be defined:
*
* - java.version
- Java version number
*
- java.version.date
- Java version date
*
- java.vendor
- Java vendor specific string
*
- java.vendor.url
- Java vendor URL
*
- java.vendor.version
- Java vendor version
*
- java.home
- Java installation directory
*
- java.class.version
- Java class version number
*
- java.class.path
- Java classpath
*
- os.name
- Operating System Name
*
- os.arch
- Operating System Architecture
*
- os.version
- Operating System Version
*
- file.separator
- File separator ("/" on Unix)
*
- path.separator
- Path separator (":" on Unix)
*
- line.separator
- Line separator ("
" on Unix)
* - user.name
- User account name
*
- user.home
- User home directory
*
- user.dir
- User's current working directory
*
*/
Properties properties = System.getProperties();
Enumeration names= properties.propertyNames();
while(names.hasMoreElements()) {
String key = names.nextElement().toString();
System.out.println(key+" : "+properties.getProperty(key));
}
}
```
:
```java
java.runtime.name : OpenJDK Runtime Environment
sun.boot.library.path : /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64
java.vm.version : 25.222-b10
java.vm.vendor : Private Build
java.vendor.url : http://java.oracle.com/
path.separator : :
java.vm.name : OpenJDK 64-Bit Server VM
file.encoding.pkg : sun.io
user.country : GB
sun.java.launcher : SUN_STANDARD
sun.os.patch.level : unknown
java.vm.specification.name : Java Virtual Machine Specification
user.dir : /home/thinkpad/my-repo/train
java.runtime.version : 1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10
java.awt.graphicsenv : sun.awt.X11GraphicsEnvironment
java.endorsed.dirs : /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/endorsed
os.arch : amd64
java.io.tmpdir : /tmp
line.separator :
java.vm.specification.vendor : Oracle Corporation
os.name : Linux
sun.jnu.encoding : UTF-8
java.library.path : /usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib
java.specification.name : Java Platform API Specification
java.class.version : 52.0
sun.management.compiler : HotSpot 64-Bit Tiered Compilers
os.version : 5.0.0-32-generic
user.home : /home/thinkpad
user.timezone :
java.awt.printerjob : sun.print.PSPrinterJob
file.encoding : UTF-8
java.specification.version : 1.8
user.name : thinkpad
java.class.path : /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/icedtea-sound.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/java-atk-wrapper.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar:/home/thinkpad/my-repo/train/target/classes:/home/thinkpad/software/idea-fly/lib/idea_rt.jar
java.vm.specification.version : 1.8
sun.arch.data.model : 64
java.home : /usr/lib/jvm/java-8-openjdk-amd64/jre
sun.java.command : com.huyouxiao.train.PropertiesTest
java.specification.vendor : Oracle Corporation
user.language : en
awt.toolkit : sun.awt.X11.XToolkit
java.vm.info : mixed mode
java.version : 1.8.0_222
java.ext.dirs : /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path : /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jfr.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/classes
java.vendor : Private Build
file.separator : /
java.vendor.url.bug : http://bugreport.sun.com/bugreport/
sun.cpu.endian : little
sun.io.unicode.encoding : UnicodeLittle
sun.desktop : gnome
sun.cpu.isalist :
```