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

时间:2011-10-31 19:37:33

标签: java

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);
    }

我正在尝试使用保存在单独目录中的代码之外的资源包来加载属性文件。但是当我尝试这样做时(上面的代码)我得到错误

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

如果我在src / main / resources文件夹中保留此文件filters.properties,那么这段代码工作正常...但是当我把它保留在外面它不起作用..不知道为什么.. < / p>

CONFIG_DIR包含\ my \ dir \ conf,FILTERS_FILE包含filters.properties文件。

FILTERS_FILE的值为filters.properties,FILTERS_BASE的值为filters,网址的值为[file:/C:/my/dir/conf/filters.properties]

filter.properties文件位于/my/dir/conf/filters.properties

1 个答案:

答案 0 :(得分:2)

尝试让文件指向目录而不是实际的属性文件。所以只需将该方法的第一个语句更改为

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

如果FILTERS_BASE包含filters,那就足够了。您不需要完整的filters.properties名称,因为。getBundle方法会附加.properties前缀。

相关问题