[英]Can we specify directory path with the property file while using ResourceBundle class in Java?
I want to place my properties files in some folder, but I am not able to read them because we can specify only the bundle name in static getBundle()
method on ResourceBundle object. 我想将我的属性文件放在某个文件夹中,但我无法读取它们,因为我们只能在ResourceBundle对象的静态getBundle()
方法中指定包名称。
Suppose bundle is: myFile.properties 假设bundle是: myFile.properties
Current path is: src 当前路径是: src
I want to keep my properties file in: src/temp 我想保留我的属性文件: src / temp
So when I am using: 所以当我使用时:
ResourceBundle.getBundle("temp/myfile", currentLocale);
it is throwing an exception "can't find bundle". 它抛出异常“找不到捆绑”。 I want some way to specify the path. 我想要一些方法来指定路径。 Please suggest me some way to do this. 请建议我这样做的一些方法。
Thank you 谢谢
Use this: 用这个:
ResourceBundle.getBundle("temp.myfile", currentLocale);
The baseName
supplied in the ResourceBundle.getBundle
call is supposed to be a fully qualified class name. ResourceBundle.getBundle
调用中提供的baseName
应该是完全限定的类名。 So it has to be written separated with dots. 所以必须用点分隔。 Also note that this makes temp
a package in your java code (which I don't think is a good idea). 另外请注意,这使得temp
在Java代码的包(我不认为这是一个好主意)。 It is better to put the properties file in a proper package like com.xyz.abc
. 最好将属性文件放在com.xyz.abc
这样的适当包中。 Then you can access it using 然后你可以使用它来访问它
ResourceBundle.getBundle("com.xyz.abc.myfile", currentLocale);
It is possible that you are using the wrong filename (myfile != myFile). 您可能使用了错误的文件名(myfile!= myFile)。
For a file myFile.properties
in a directory temp
on the classpath, this code should work: 对于类路径上目录temp
的文件myFile.properties
,此代码应该有效:
ResourceBundle.getBundle("temp.myFile");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.