关于java中System.Properties

java.util.Properties


前言

jvm启动后会自动加载很多系统属性,会在之后必要的时候会通过System.getProperty("property")来获取,通过

System.setProperty(key, value)来设置系统属性。


一、System.Properties是什么?

先上一段 jdk1.8的java.util.Properties的官方doc。

public class Properties extends Hashtable<Object,Object>

The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

A property list can contain another property list as its "defaults"; this second property list is searched if the property key is not found in the original property list.

Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail. Similarly, the call to the propertyNames or list method will fail if it is called on a "compromised" Properties object that contains a non-String key.

The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes as defined in section 3.3 of The Java™ Language Specification; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

The Properties.loadFromXML(InputStream) and Properties.storeToXML(OutputStream, String, String) methods load and store properties in a simple XML format. By default the UTF-8 character encoding is used, however a specific encoding may be specified if required. Implementations are required to support UTF-8 and UTF-16 and may support other encodings. An XML properties document has the following DOCTYPE declaration:

 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
 

Note that the system URI (http://java.sun.com/dtd/properties.dtd) is not accessed when exporting or importing properties; it merely serves as a string to uniquely identify the DTD, which is:

    <?xml version="1.0" encoding="UTF-8"?>

    <!-- DTD for properties -->

    <!ELEMENT properties ( comment?, entry* ) >

    <!ATTLIST properties version CDATA #FIXED "1.0">

    <!ELEMENT comment (#PCDATA) >

    <!ELEMENT entry (#PCDATA) >

    <!ATTLIST entry key CDATA #REQUIRED>
 

This class is thread-safe: multiple threads can share a single Properties object without the need for external synchronization.

日付:

JDK1.0

参照:

native2ascii tool for Solaris, native2ascii tool for Windows

继承Hashtable<Object,Object>

1.使用例 

代码如下(示例):

// 获取当前所有的propertities
Properties p = System.getProperties();
p.forEach((k, v) -> {
    System.out.println("K:" + k + " V:" + v);
});

// 设置自定义property
System.setProperty("myTestProperty","domain.test.Node");
// 获取自定义property
String propertyVal = System.getProperty("myTestProperty");
System.out.println(propertyVal);

 输出结果
 

K:sun.cpu.isalist V:amd64
K:sun.desktop V:windows
K:sun.io.unicode.encoding V:UnicodeLittle
K:sun.cpu.endian V:little
K:java.vendor.url.bug V:http://bugreport.sun.com/bugreport/
K:file.separator V:\
K:java.vendor V:Oracle Corporation
K:sun.boot.class.path V:C:\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Java\jdk1.8.0_181\jre\lib\rt.jar;C:\Java\jdk1.8.0_181\jre\lib\sunrsasign.jar;...
K:java.ext.dirs V:C:\Java\jdk1.8.0_181\jre\lib\ext;C:\Windows\Sun\Java\lib\ext
K:java.version V:1.8.0_181
K:java.vm.info V:mixed mode
K:awt.toolkit V:sun.awt.windows.WToolkit
K:user.language V:ja
K:java.specification.vendor V:Oracle Corporation
K:sun.java.command V:com.dangxk.tools.test.TestSysProper
K:java.home V:C:\Java\jdk1.8.0_181\jre
K:sun.arch.data.model V:64
K:java.vm.specification.version V:1.8
K:java.class.path V:D:\NetBeansProjects\tools\target\classes;C:\Users\dangxk.SRK01\.m2\repository\org\...
K:user.name V:dangxk
K:file.encoding V:MS932
K:java.specification.version V:1.8
K:java.awt.printerjob V:sun.awt.windows.WPrinterJob
K:user.timezone V:
K:user.home V:C:\Users\xxx
K:os.version V:6.1
K:sun.management.compiler V:HotSpot 64-Bit Tiered Compilers
K:java.specification.name V:Java Platform API Specification
K:java.class.version V:52.0
K:java.library.path V:C:\Java\jdk1.8.0_181\bin;C:\Windows\Sun\Java\bin;...
K:sun.jnu.encoding V:MS932
K:os.name V:Windows 7
K:user.variant V:
K:java.vm.specification.vendor V:Oracle Corporation
K:java.io.tmpdir V:C:\Users\xxx\AppData\Local\Temp\
K:line.separator V:

K:java.endorsed.dirs V:C:\Java\jdk1.8.0_181\jre\lib\endorsed
K:os.arch V:amd64
K:java.awt.graphicsenv V:sun.awt.Win32GraphicsEnvironment
K:java.runtime.version V:1.8.0_181-b13
K:java.vm.specification.name V:Java Virtual Machine Specification
K:user.dir V:D:\NetBeansProjects\tools
K:user.country V:JP
K:user.script V:
K:sun.java.launcher V:SUN_STANDARD
K:sun.os.patch.level V:Service Pack 1
K:java.vm.name V:Java HotSpot(TM) 64-Bit Server VM
K:file.encoding.pkg V:sun.io
K:path.separator V:;
K:java.vm.vendor V:Oracle Corporation
K:java.vendor.url V:http://java.oracle.com/
K:sun.boot.library.path V:C:\Java\jdk1.8.0_181\jre\bin
K:java.vm.version V:25.181-b13
K:java.runtime.name V:Java(TM) SE Runtime Environment

总结

常见的如  ClassLoader 类,Class类都有使用到系统属性

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值