关于资源文件路径的问题
大侠们,我最近在做的国际化的工作,需要用到一组的Properties文件(就是i18n)的标准.
这样再去取这些资源的时候,要写上绝对或者相对路径.
这样我觉得会很麻烦.能不能通过设置classpath的方法找到这些资源文件?
就是说,我为整个工程而不是每一个子目录生成一组Properties,所有的程序都从同一位置去取这些资源.
例如:
我的程序TestResourceBundle
import java.util.*;
public class TestResourceBundle {
private static ResourceBundle resources;
static {
try {
resources = ResourceBundle.getBundle("resources.rcmsgs",
Locale.getDefault());
} catch (MissingResourceException mre) {
System.err.println("resources/rcmsgs.properties not found");
System.exit(1);
}
}
public static void main(String[] args) {
String key = getResourceString("W2_UI_SERVICEPRODUCT");
System.out.println(key);
}
protected static String getResourceString(String nm) {
String str;
try {
str = resources.getString(nm);
} catch (MissingResourceException mre) {
str = null;
}
return str;
}
}
资源文件1:rcmsgs.properties
W2_UI_SERVICEPRODUCT = Service Product Manager
#服务产品管理器
资源文件2:rcmsgs_zh.properties
W2_UI_SERVICEPRODUCT = \u670d\u52a1\u4ea7\u54c1\u7ba1\u7406\u5668