如何从java中的目录获取资源包文件?

时间:2013-11-26 16:55:20

标签: java internationalization

我有一个国际化应用程序的代码。我需要加载捆绑文件,从运行位置返回两次并加载它。

我的代码是,

bundle = ResourceBundle.getBundle("../../resources/basic",new Locale("fr", "CA"));
lblUsername.setText(bundle.getString("username"));
lblPassword.setText(bundle.getString("password"));
btnLogin.setText(bundle.getString("login"));

我收到了以下错误。

java.util.MissingResourceException: Can't find bundle for base name ../../resources/basic, locale fr_CA
at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
at java.util.ResourceBundle.getBundleImpl(Unknown Source)
at java.util.ResourceBundle.getBundle(Unknown Source)
at com.daycare.ui.user.Login$4.itemStateChanged(Login.java:248)
at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at com.daycare.ui.user.Login.<init>(Login.java:372)
at com.daycare.ui.user.Login$1.run(Login.java:104)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

如何提供正确的budle文件路径?

提前致谢!

3 个答案:

答案 0 :(得分:8)

据我所知,Bundle类默认会在当前ClassLoader上查找您的资源。如果你想查看文件系统上的文件,请改用它:

File file = new File("the path of the folder containing the bundles");
URL[] urls = new URL[]{file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle rb = ResourceBundle.getBundle("the bundle name", your_locale, loader);

答案 1 :(得分:1)

ResourceBundle.getBundle("../../resources/basic",new Locale("fr", "CA")); 

将第一个参数更改为完全限定的类名,并确保basic_fr_CA.properties存在。

例如,

如果您的文件位于

/resource/basic_fr_CA.properties location, 

然后将您的Java代码更改为

ResourceBundle.getBundle("resources.basic",new Locale("fr", "CA"));

答案 2 :(得分:0)

如果你的资源在resources文件夹下,可以这样使用,这样比较好,不用处理checked异常:

ClassLoader loader = new URLClassLoader(new URL[]{ I18NManager.class.getResource("path to folder containing bundles you want to load")});
ResourceBundle rb = ResourceBundle.getBundle("the bundle name", locale);

其中 I18NManager 表示正在加载资源的类。

如果

I18NManager.class 不在静态上下文中,可以用 getClass() 替换。