Java.util.Property ResourceBundle.handleGetObject() 方法
描述
java.util.PropertyResourceBundle.handleGetObject(String key) 方法返回包中指定键的对象。 如果包不包含所需的对象,则该方法返回 null。
声明
以下是 java.util.PropertyResourceBundle.handleGetObject() 方法的声明
public Object handleGetObject(String key)
参数
键 − 所需对象的键。
返回值
此方法返回指定键的对象。
异常
NA
示例
下面的例子展示了 java.util.PropertyResourceBundle.handleGetObject(String) 方法的使用。
package com.tutorialspoint;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.PropertyResourceBundle;
public class PropertyResourceBundleDemo {
public static void main(String[] args) {
// Prepare content for simulating property files
String fileContent =
"# Integer value 1\n" +
"s1 = 1\n" +
"# String value PropertyResourceBundleDemo\n" +
"s2 = PropertyResourceBundleDemo\n" +
"# Date value Fri Jan 31 00:00:00 IST 3913\n" +
"s3 = Fri Jan 31 00:00:00 IST 3913";
InputStream propStream = new StringBufferInputStream(fileContent);
try {
// Create property resource bundle
PropertyResourceBundle bundle =
new PropertyResourceBundle(propStream);
// Get resources
Object res1 = bundle.handleGetObject("s1");
Object res2 = bundle.handleGetObject("s2");
Object res3 = bundle.handleGetObject("s3");
// Print resource contents
System.out.println("[Resource1] "+res1);
System.out.println("[Resource2] "+res2);
System.out.println("[Resource3] "+res3);
} catch (IOException e) {
e.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 −
[Resource1] 1 [Resource2] PropertyResourceBundleDemo [Resource3] Fri Jan 31 00:00:00 IST 3913