简体   繁体   English

在Java中使用ResourceBundle类时,我们可以使用属性文件指定目录路径吗?

[英]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.

相关问题 将resourceBundle与外部文件Java结合使用 - Using resourceBundle with an external file, java 通过ResourceBundle访问属性文件时出错 - Error while accessing property file by ResourceBundle 解析属性文件Java ResourceBundle的环境变量 - Resolve environment variables of property file Java ResourceBundle 如何在属性文件中指定值,以便可以使用ResourceBundle#getStringArray检索它们? - How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray? 无法在Java中指定类路径 - Can't specify class path in Java 我可以为Java中的目录指定“基本程序包路径”吗? - Can I specify a “base package path” for a directory in Java? 如何在运行Java应用程序时使用classloader将目录指定为classpath以读取其中的文件? - How to specify the directory as classpath in order to read a file in it using classloader while running java application? 使用从 Java 加载 class 导入属性文件时出错 - Error while using the load class from Java to import property file 如何在将文件移动到Java中的另一个目录时为文件指定新名称? - How to specify a new name for a file while moving it to another directory in Java? 如何从jersey的属性文件中指定路径值? - How can i specify path value from property file in jersey?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM