Java Properties getProperty()方法

java.util.Properties.getProperty(String key,String defaultValue) 与在此属性列表中指定键的属性方法搜索。如果在此属性列表中找不到该键,则将递归检查默认属性列表及其默认值。如果找不到该属性,则该方法返回默认值参数。

1 语法

public String getProperty(String key,String defaultValue)

2 参数

key:需要查询的键。

defaultValue:默认值。

3 返回值

返回此属性列表中具有指定键值的值。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Properties.getProperty(String key,String defaultValue) 方法的例子
 */
import java.util.*;

public class Demo {
    public static void main(String[] args) {
        Properties prop = new Properties();

        // add some properties
        prop.put("Height", "200");
        prop.put("Width", "150");
        prop.put("Scannable", "true");

        // get two properties and print them
        System.out.println("" + prop.getProperty("Scannable","false"));
        System.out.println("" + prop.getProperty("Width","150"));
    }
}

输出结果为:

true
150

热门文章

优秀文章