java properties.getProperty(key) 方法返回空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25080002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:18:06  来源:igfitidea点击:

properties.getProperty(key) method returns null value

javaswingjakarta-eeproperties-file

提问by kannan arumugam

I am load the properties file and get the value from that file but when i using "Properties"class and getProperty(key)method, it was return nullvalue.

我正在加载属性文件并从该文件中获取值,但是当我使用“属性”类和getProperty(key)方法时,它返回值。

code:

代码:

public class LoadPropertiesFile {

public static String getProperty (String key, String filePath) {
    Properties properties = new Properties();
    InputStream inputStream = null;
    String value = null;
    try {
        String appHome = ConfigUtil.getApplicationHome() + filePath; 
        inputStream = new FileInputStream(appHome);

        //load a properties file
        properties.load(inputStream);

        //get the property value 
        System.out.println(properties.getProperty("7"));   //print **Unlock**
        System.err.println(key);   //print **7**
        System.out.println(value);   //print **null**
        value = properties.getProperty(key);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
    return value;
}
}

Output:

输出:

Unlock
7 
null

Property File:

属性文件:

2=Interactive
3=Network
4=Batch
5=Service
7=Unlock
8=Network Cleartext
10=Remote Desktop
11=Logon with cached credentials

call method:

调用方法:

logonType = new LoadPropertiesFile().getProperty("7", "path");

When i call that method it will return nullvalue only. please help me guys.

当我调用该方法时,它只会返回值。请帮帮我伙计们。

回答by flotothemoon

You are initalizing value with null.

您正在使用 null 初始化值。

String value = null;

And you assign it afterprinting it:

打印分配它:

System.out.println(value);
value = properties.getProperty(key);

Output:null

输出:null

So value can only be null when you print it, as you never change its value until System.out.println(value);. Just switch those two statements:

因此 value 只能在打印时为 null,因为在System.out.println(value);. 只需切换这两个语句:

value = properties.getProperty(key);
System.out.println(value);

Output:unlock

输出:unlock

Edit

编辑

properties.getProperty(key) may return null too, but only if there is no such key in its table, otherwise it will return the assigned value, in your example unlock.

properties.getProperty(key) 也可能返回 null,但前提是它的表中没有这样的键,否则它将返回分配的值,在您的示例中unlock

See the API Documentation on this for more details:

有关更多详细信息,请参阅 API 文档:

public String getProperty(String key)

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 null if the property is not found.

公共字符串getProperty(字符串键)

在此属性列表中搜索具有指定键的属性。如果在此属性列表中找不到该键,则递归地检查默认属性列表及其默认值。如果未找到该属性,该方法将返回 null。

http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#getProperty(java.lang.String)

http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#getProperty(java.lang.String)

回答by BackSlash

System.out.println(value);   //print **null**
value = properties.getProperty(key);

Switch these two lines and initialize valuebeforeprinting it:

切换这两行并value打印之前进行初始化:

value = properties.getProperty(key);
System.out.println(value);   //print Unlock