Java.util.ResourceBundle.handleKeySet() 方法
描述
java.util.ResourceBundle.handleKeySet() 方法返回仅包含在此 ResourceBundle 中的键的 Set。
声明
以下是 java.util.ResourceBundle.handleKeySet() 方法的声明
protected Set<String> handleKeySet()
参数
NA
返回值
此方法返回仅包含在此 ResourceBundle 中的一组键
异常
NA
示例
下面的例子展示了 java.util.ResourceBundle.handleKeySet() 方法的使用。
package com.tutorialspoint;
import java.util.*;
public class ResourceBundleDemo extends ResourceBundle {
@Override
public Object handleGetObject(String key) {
if (key.equals("hello")) {
return "Hello World!";
} else {
return null;
}
}
@Override
public Enumeration getKeys() {
StringTokenizer key = new StringTokenizer("Hello World!");
return key;
}
protected Set<String> handleKeySet() {
return new HashSet<String>();
}
public static void main(String[] args) {
// create a new ResourceBundle with specified locale
ResourceBundle bundle =
ResourceBundle.getBundle("hello", Locale.US);
// print the keys
Enumeration<String> enumeration = bundle.getKeys();
while (enumeration.hasMoreElements()) {
System.out.println("" + enumeration.nextElement());
}
}
}
假设我们在您的 CLASSPATH 中有一个可用的资源文件 hello_en_US.properties,其内容如下。 该文件将用作我们示例程序的输入 −
hello = Hello World! bye = Goodbye World! morning = Good Morning World!
让我们编译并运行上面的程序,这将产生以下结果 −
hello bye morning

