我想读取位于我的代码的jar文件外部的属性。使用ResourceBundle
但它无法读取属性文件locations.properties
。属性文件位于resource
文件夹下,并且jar
和resource
文件夹都位于同一目录下。在罐子外面找不到资源
myDir
--> myJar.jar
--> resource
-->locations.properties
我不知道什么是错下面我的代码:
public static ResourceBundle getResourceBundle(String fileName) throws MalformedURLException{
if (resourceBundle == null) {
File file = new File("resource/"+fileName);
URL[] urls = { file.toURI().toURL() };
ClassLoader loader = new URLClassLoader(urls);
resourceBundle = ResourceBundle.getBundle(fileName, Locale.getDefault(), loader);
}
return resourceBundle;
}
这是怎么了调用ResourceBundle
对象:
ResourceBundle locationBundle = null;
try {
locationBundle = ReadPropertyUtil.getResourceBundle(propFileName);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
请指导什么是错,此代码和什么是读取外部属性文件的正确方法。
可能重复(http://stackoverflow.com/questions/指定的文件2343187/loading-resources-using-getclass-getresource) – CMPS