java Properties 学习笔记

本文是关于Java Properties的详细学习笔记,探讨了如何使用Properties进行配置文件读写,以及其在系统属性设置中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:
         * <dl>
         * <dt>java.version         <dd>Java version number
         * <dt>java.version.date    <dd>Java version date
         * <dt>java.vendor          <dd>Java vendor specific string
         * <dt>java.vendor.url      <dd>Java vendor URL
         * <dt>java.vendor.version  <dd>Java vendor version
         * <dt>java.home            <dd>Java installation directory
         * <dt>java.class.version   <dd>Java class version number
         * <dt>java.class.path      <dd>Java classpath
         * <dt>os.name              <dd>Operating System Name
         * <dt>os.arch              <dd>Operating System Architecture
         * <dt>os.version           <dd>Operating System Version
         * <dt>file.separator       <dd>File separator ("/" on Unix)
         * <dt>path.separator       <dd>Path separator (":" on Unix)
         * <dt>line.separator       <dd>Line separator ("\n" on Unix)
         * <dt>user.name            <dd>User account name
         * <dt>user.home            <dd>User home directory
         * <dt>user.dir             <dd>User's current working directory
         * </dl>
         */
        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 : 
```


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值