Java ResourceBundle.getBundle("ResourceFile", new Locale("us", "US")) 在哪里查找文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2344483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where does ResourceBundle.getBundle("ResourceFile", new Locale("us", "US")) look for the file?
提问by Craig A
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?
我正在运行 Eclipse 并尝试创建一个简单的测试程序,尝试使用几个不同的文件来尝试我们的 ResourceBundle。该文件正确命名为 ResourceFile_us_US.properties。但是我在 getBundle() 调用中遇到异常,因为它显然找不到该文件。应该在哪里找到它?
回答by Alex Beardsley
I believe it just looks in the classpath.
我相信它只是在类路径中查找。
回答by tmwanik
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:
您知道 java 正在寻找特定语言环境中的属性文件。您可能会感到困惑,为什么 java 一直抱怨它找不到就在那里的属性文件。调试此类错误时要记住以下几点:
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"
这些资源属性文件由类加载器加载,类似于java类。因此您需要将它们包含在您的运行时类路径中。
这些资源具有完全限定资源名称,类似于完全限定类名称,摘录您不能将资源导入到您的 java 源文件中。为什么?因为它的名字采用字符串的形式。
ResourceBundle.getBundle("config")
告诉类加载器加载一个以"config"
默认包命名的资源(即没有包)。它并不意味着当前包中具有引用类的资源。ResourceBundle.getBundle("com.cheng.scrap.config")
告诉类加载器加载一个以"config"
包命名的资源"com.cheng.scrap."
它的完全限定资源名称是"com.cheng.scrap.config"
More : Can't find bundle for base name com...config, locale zh_CN
更多:找不到基本名称 com...config、语言环境 zh_CN 的包
Cheers.
干杯。
回答by Shinx
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 as ResourceBundle resourceBundle = ResourceBundle.getBunndle("ResourceBundle", Locale.getDefault());
In your classpath. Make sure the calsspath environment variable is set
/src/resources 确保在资源包文件的名称中包含语言环境。例如,对于津巴布韦,它将是 ResourceBundle_en_ZW.properties,您可以将其加载为 ResourceBundle resourceBundle = ResourceBundle.getBunndle("ResourceBundle", Locale.getDefault());
在你的类路径中。确保设置了 calsspath 环境变量
回答by Adil
If you create a package resources
and put the file hello_en_US.properties
inside it, which has the content:
如果您创建一个包resources
并将文件hello_en_US.properties
放入其中,其中包含以下内容:
hello = Hello World!!
你好=世界你好!!
you can print the content of hello using the following code:
您可以使用以下代码打印 hello 的内容:
package localization;
import java.util.Locale;
import java.util.ResourceBundle;
public class ResourceBundleDemo {
public static void main(String[] args) {
Locale en_US = new Locale("en", "US");
ResourceBundle bundle = ResourceBundle.getBundle("resources.hello", en_US);
// print the value of the key "hello"
System.out.println("" + bundle.getString("hello"));
}
}