ResourceBundle.getBundle(“ResourceFile”, new Locale(“us”, “US”)) 在哪里查找文件?
我正在运行 Eclipse 并尝试创建一个简单的测试程序,尝试使用几个不同的文件来尝试我们的 ResourceBundle。该文件正确命名为 ResourceFile_us_US.properties。但我在 getBundle() 调用上遇到异常,因为它显然找不到该文件。它应该位于哪里以便可以找到?
I'm running Eclipse and trying to create a simple test program trying our ResourceBundle with a couple of different files. The file is properly named as ResourceFile_us_US.properties. But I'm getting an exception on the getBundle() call because it apparently can't find the file. Where should it be located so it can be found?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您知道 java 正在寻找特定语言环境中的属性文件。您可能会感到困惑,为什么 java 一直抱怨它找不到那里的属性文件。调试此类错误时需要记住以下几点:
这些资源属性文件由类加载器加载,类似于 java 类。因此,您需要将它们包含在运行时类路径中。
这些资源具有完全限定资源名称,类似于完全限定类名称,摘录您无法将资源导入到 java 源文件中。为什么?因为它的名称采用字符串的形式。
ResourceBundle.getBundle("config")
告诉类加载器使用默认包(即无包)加载名为"config"
的资源。它并不意味着当前包中具有引用类的资源。ResourceBundle.getBundle("com. Cheng.scrap.config")
告诉类加载器加载一个名为"config"
的资源,其包为"com. heng.scrap."
其完全限定资源名称是"com. Cheng.scrap.config"
更多:找不到基础包名称 com...config,区域设置 zh_CN
干杯。
You know java is looking for a properties file in a specific locale. You may be baffled why java keeps complaining it can't find a properties file that is right there. A few things to keep in mind when debugging this type of errors:
These resource properties files are loaded by classloader, similar to java classes. So you need to include them in your runtime classpath.
These resources have fully-qualified-resource-name, similar to a fully-qualified-class-name, excerpt you can't import a resource into your java source file. Why? because its name takes the form of a string.
ResourceBundle.getBundle("config")
tells the classloader to load a resource named"config"
with default package (that is, no package). It does NOT mean a resource in the current package that has the referencing class.ResourceBundle.getBundle("com.cheng.scrap.config")
tells the classloader to load a resource named"config"
with package"com.cheng.scrap."
Its fully-qualified-resource-name is"com.cheng.scrap.config"
More : Can't find bundle for base name com...config, locale zh_CN
Cheers.
如果您创建一个包
resources
并放入文件hello_en_US.properties
里面有内容:
您可以使用以下代码打印 hello 的内容:
If you create a package
resources
and put the filehello_en_US.properties
inside it, which has the content:
you can print the content of hello using the following code:
两者之一:
/src/resources
确保在资源包文件的名称中包含区域设置。例如,对于津巴布韦,它将是
ResourceBundle_en_ZW.properties
并且您可以将其加载为ResourceBundle resourceBundle = ResourceBundle.getBunndle("ResourceBundle", Locale.getDefault());
在你的类路径中。确保设置了类路径环境变量
One of two:
/src/resources
Make sure you include locale in name of resource bundle file. e.g for Zimbabwe, it will be
ResourceBundle_en_ZW.properties
and you would load it asResourceBundle resourceBundle = ResourceBundle.getBunndle("ResourceBundle", Locale.getDefault());
In your classpath. Make sure the classpath environment variable is set
我相信它只是在类路径中查找。
I believe it just looks in the classpath.