无法使用ResourceBundle从文件系统加载文件

问题描述:

public static void loadFilters() throws MalformedURLException { 
     File filtersFile = new File(CONFIG_DIR + "/" + FILTERS_FILE); 
     URL[] urls = {filtersFile.toURI().toURL()}; 
     ClassLoader loader = new URLClassLoader(urls); 
     ResourceBundle bundle = ResourceBundle.getBundle(FILTERS_BASE, Locale.getDefault(), loader); 
     if (StringUtils.isNotBlank(getStringValue(bundle, ALLOW_TYPE_PATTERN_KEY))) { 
      ALLOWED_TYPES = Pattern.compile(getStringValue(bundle, ALLOW_TYPE_PATTERN_KEY)); 
     } 
     if (StringUtils.isNotBlank(getStringValue(bundle, DENY_TYPE_PATTERN_KEY))) { 
      DENIED_TYPES = Pattern.compile(getStringValue(bundle, DENY_TYPE_PATTERN_KEY)); 
     } 
     ALLOWED_MIME_TYPES = getListValue(bundle, ALLOW_MIME_PATTERN_KEY); 
     DENIED_MIME_TYPES = getListValue(bundle, DENY_MIME_PATTERN_KEY); 
    } 

我想加载属性文件,使用保存在单独目录中的代码之外的资源包。但是,当我尝试做这样(上面的代码)我得到错误的无法使用ResourceBundle从文件系统加载文件

ERROR [main] Can't find bundle for base name filters, locale en_US 

而且如果我保持这个文件filters.properties中的src/main /资源文件夹,然后这个代码是工作的罚款...但是当我将它保留在外面时,它不起作用..不知道为什么..不知道为什么..

而CONFIG_DIR包含\ my \ dir \ conf并且FILTERS_FILE包含filters.properties文件。

FILTERS_FILE具有价值filters.properties和FILTERS_BASE具有价值filters和网址获得了价值[file:/C:/my/dir/conf/filters.properties]

而且filters.properties文件是/my/dir/conf/filters.properties

+0

“FILTERS_FILE”和“FILTERS_BASE”的值是什么?前者是指向一个目录还是一个文件? 'URLClassLoader'假设URL指向一个目录,如果以'/'结尾,否则它假定你指向一个jar文件。 –

+0

@G_H,我已经更新了问题..和FILTERS_FILE具有值filters.properties和FILTERS_BASE具有值过滤器和URL获得值为[文件:/ C:/my/dir/conf/filters.properties] – ferhan

尝试具有文件点到目录而不是实际的属性文件。因此,只要改变方法的第一个语句

File filtersFile = new File(CONFIG_DIR + "/"); 

如果FILTERS_BASE包含filters,这应该是足够的。由于.properties前缀附加在getBundle方法中,因此不需要完整的filters.properties名称。

+0

感谢它的工作.. !! :) – ferhan