【发布时间】:2014-09-24 15:26:58
【问题描述】:
我正在加载属性文件并从该文件中获取值,但是当我使用 "Properties" 类和 getProperty(key) 方法时,它返回 null 值。
代码:
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;
}
}
输出:
Unlock
7
null
属性文件:
2=Interactive
3=Network
4=Batch
5=Service
7=Unlock
8=Network Cleartext
10=Remote Desktop
11=Logon with cached credentials
调用方法:
logonType = new LoadPropertiesFile().getProperty("7", "path");
当我调用该方法时,它只会返回 null 值。请帮帮我。
【问题讨论】:
-
您希望
int i; System.out.println(i); i = 5;打印什么? (不要尝试;猜测) -
@immibis 我想你想说*打印 5,而不仅仅是打印 ;)
-
@1337 这是一个问题。 “你希望
做什么?” -
@immibis 你编辑了吗? O_o 我发誓之前没有“什么”……;)
标签: java swing jakarta-ee properties-file