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

如何在java中从目录中获取资源包文件?,java,internationalization,Java,Internationalization,我有一个国际化应用程序的代码。我需要加载bundle文件,从运行位置返回两次并加载它 我的密码是 bundle = ResourceBundle.getBundle("../../resources/basic",new Locale("fr", "CA")); lblUsername.setText(bundle.getString("username")); lblPassword.setText(bundle.getString("password")); btnLogin.setText

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

我的密码是

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)
java.util.MissingResourceException:找不到基本名称.././resources/basic的捆绑包,区域设置fr\u CA
位于java.util.ResourceBundle.throwMissingResourceException(未知源)
位于java.util.ResourceBundle.getBundleImpl(未知源)
位于java.util.ResourceBundle.getBundle(未知源)
在com.daycare.ui.user.Login$4.itemStateChanged(Login.java:248)
位于javax.swing.JComboBox.fireItemStateChanged(未知源)
在javax.swing.JComboBox.selectedItemChanged(未知源)
位于javax.swing.jcombox.contentsChanged(未知源)
位于javax.swing.AbstractListModel.fireContentsChanged(未知源)
位于javax.swing.DefaultComboxModel.setSelectedItem(未知源)
位于javax.swing.JComboBox.setSelectedItem(未知源)
位于javax.swing.JComboBox.setSelectedIndex(未知源)
在com.daycare.ui.user.Login.(Login.java:372)
在com.daycare.ui.user.Login$1.run(Login.java:104)
在java.awt.event.InvocationEvent.dispatch(未知源)处
位于java.awt.EventQueue.dispatchEventImpl(未知源)
位于java.awt.EventQueue.access$200(未知源)
在java.awt.EventQueue$3.run处(未知源)
在java.awt.EventQueue$3.run处(未知源)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(未知源)
位于java.awt.EventQueue.dispatchEvent(未知源)
位于java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)
位于java.awt.EventDispatchThread.pumpEventsForFilter(未知源)
位于java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源)
位于java.awt.EventDispatchThread.pumpEvents(未知源)
位于java.awt.EventDispatchThread.pumpEvents(未知源)
位于java.awt.EventDispatchThread.run(未知源)
如何给出budle文件的正确路径


提前谢谢

据我所知,默认情况下,
类将在当前的
类加载器
上查找您的资源。如果要查看文件系统上的文件,请使用该文件:

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);
将第一个参数更改为完全限定类名,并确保存在基本的\u fr\u CA.properties

比如说 如果您的文件位于

/resource/basic_fr_CA.properties location, 
然后将java代码更改为

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

如果您的资源位于resources文件夹中,您可以使用这种方式,这样做更好,因为您不必处理选中的异常:

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()

您的捆绑包是否存在于指定位置,您的资源捆绑包的名称是什么?基本\u fr\u CA。属性存在于指定位置是存在。文件名是基本的。请给我一个电话号码
ResourceBundle
在包中,即JAR文件中的文件夹;不是目录。这可能有助于你澄清问题。我犯了这个错误。java.util.MissingResourceException:找不到基本名称basic、locale en_USCA的捆绑包。您可以尝试使用捆绑包目录的绝对路径,而不是本文中描述的相对路径。并且您确定您的文件名为basic_en_.properties?是的。当然它是basic_en_USit必须以.properties结尾如果文件位于/resource/basic_fr_CA.properties位置,则将java代码更改为ResourceBundle.getBundle(“resources.basic”,新区域设置(“fr”,“CA”);是的。但同样的错误。java.util.MissingResourceException:找不到基本名称basic的捆绑包,locale fr_Cai是类路径中的资源文件夹。此外,如果资源束与类位于同一目录中,请删除所有路径信息。是。“我的资源”文件夹包含basic.properties、basic_en_US.properties、basic_fr_CA.properties文件和名为MainLogo的jpg图像。这是因为输入错误资源和资源文件夹是不同的
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);