ResourceBundle 抛出 MissingResourceException

ResourceBundle throws MissingResourceException

我正在尝试从文件位置读取属性文件。但是,资源包找不到文件并抛出以下异常

我尝试将文件保存在 class 路径下,但未能成功读取文件。但是,我的目的是从文件路径读取 属性 文件,这来自用户。因此,我的 class 可以读取不可知的文件输入以获取属性。

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name configFileName, locale en_US
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:721)
    at com.cisco.propertiesreader.FileConfigLoader.getBundle(FileConfigLoader.java:16)
    at com.cisco.propertiesreader.MainApp.main(MainApp.java:10)

FileConfigLoader class:

package com.cesna.propertiesreader;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

public class FileConfigLoader {

    private static final Logger LOGGER = Logger.getLogger(FileConfigLoader.class);

    private static String EMPTY = "";

    public static ResourceBundle getBundle(String configLocation) {

        ResourceBundle rb = ResourceBundle.getBundle("configFileName");

        return rb;

    }

    public static String getValue(ResourceBundle rb, String key) {
        String value = EMPTY;

        try {
            value = rb.getString(key);
        } catch (MissingResourceException mREx) {
            LOGGER.error("Missing Resource : " + key);
        }

        return value;
    }

}

MainApp Class:

package com.cesna.propertiesreader;

import java.util.ResourceBundle;

public class MainApp {

    public static void main(String[] args) {

        ResourceBundle resource = FileConfigLoader
                .getBundle("D:\PDIWorkspace\PropertyFileReader\resoures\config.properties");

        String hive_db = FileConfigLoader.getValue(resource,"hive_db");

        System.out.println(hive_db);
    }

}

嗯,我做对了。

config.properties

hive_db=installbase
EDGE_HIVE_CONN=dev-node
target_dir=/app/dev/SmartAnalytics/sqoop_temp/
IB_log_table=IB_log
SR_DG_master_table=data_usage_governance_master
SR_DG_table=data_usage_governance_log

使用以下方式读取文件后问题已解决:

package com.cesna.propertiesreader;

import java.util.ResourceBundle;

public class MainApp {

    public static void main(String[] args) {

        ResourceBundle resource = FileConfigLoader
                .getBundle("com.cisco.propertiesreader.config");

        String hive_db = FileConfigLoader.getValue(resource,"hive_db");

        System.out.println(hive_db);
    }

}

您确定以下内容是有意的吗

public static ResourceBundle getBundle(String configLocation) {

    ResourceBundle rb = ResourceBundle.getBundle("configFileName");

    return rb;

}

我觉得应该是

public static ResourceBundle getBundle(String configLocation) {

    ResourceBundle rb = ResourceBundle.getBundle(configLocation);

    return rb;

}

如果要从外部目录(不在类路径中)加载数据

public static ResourceBundle getBundle(String basePath, String baseName) throws MalformedURLException {
    File file = new File(basePath);
    URL[] urls = {file.toURI().toURL()};
    ClassLoader loader = new URLClassLoader(urls);
    ResourceBundle rb = ResourceBundle.getBundle(baseName, Locale.getDefault(), loader);
    return rb;
}   
public static ResourceBundle getBundle(String configLocation) {
   ResourceBundle rb = ResourceBundle.getBundle("./resources/"+configLocation);
   return rb;
}