getProperty(String key) 获取指定键指示的系统属性(从系统环境或*.properties等配置文件中读取key对应的值)。
getProperty(String key, String def) 获取用指定键描述的系统属性(从系统环境或*.properties等配置文件中读取key对应的值,当key值为NULL时,返回def的值;当key值不为NULL时,返回key的值)。
其JDK源码如下:
1.public static String getProperty(String key, String def) {
checkKey(key);
SecurityManager sm = getSecurityManager();
if (sm != null) {
sm.checkPropertyAccess(key);
}
return props.getProperty(key, def);
}
2.
//当getProperty(key);为null时,返回defaultValue(即使传入的def值)
public String getProperty(String key, String defaultValue) {
String val = getProperty(key);
return (val == null) ? defaultValue : val;
}
例:
private String encoding = "GBK";
public JavaModel(Map<String, ?> environment)
{
super(environment);
//mod.encoding为system.properties配置文件中设置的键值对
this.encoding = System.getProperty("mod.encoding", this.encoding);
本文详细介绍了Java中System类的getProperty方法的使用方式及其作用。包括如何通过该方法获取系统属性,以及当属性值为空时如何设定默认返回值。此外,还提供了一个具体的示例来展示如何在实际应用中调用此方法。
2770

被折叠的 条评论
为什么被折叠?



