Can we specify directory path with the property file while using ResourceBundle class in Java?

荒凉一梦 提交于 2019-12-10 12:58:25

问题


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.

Suppose bundle is: myFile.properties
Current path is: src
I want to keep my properties file in: 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


回答1:


Use this:

ResourceBundle.getBundle("temp.myfile", currentLocale);

The baseName supplied in the ResourceBundle.getBundle call is supposed to be a fully qualified class name. 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). It is better to put the properties file in a proper package like com.xyz.abc. Then you can access it using

ResourceBundle.getBundle("com.xyz.abc.myfile", currentLocale);



回答2:


It is possible that you are using the wrong filename (myfile != myFile).

For a file myFile.properties in a directory temp on the classpath, this code should work:

ResourceBundle.getBundle("temp.myFile");


来源:https://stackoverflow.com/questions/3598240/can-we-specify-directory-path-with-the-property-file-while-using-resourcebundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!