2016-03-15 56 views
0

enter image description hereResourceBundle拋出MissingResourceException

我想從文件位置讀取屬性文件。但是,資源包無法找到該文件並拋出以下異常

我試着將文件保留在類路徑下,但沒有成功讀取文件。但是,我的意圖是從文件路徑讀取屬性文件,這來自用戶。所以,我的類可以讀取不可知文件輸入來獲取屬性。

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類:

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類:

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

} 

回答

1

你確定了以下旨在

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; 
} 
+0

我用configLocation \t公共靜態的ResourceBundle的getBundle(字符串configLocation){ \t \t資源包加載數據rb = ResourceBundle.getBundle(configLocation); \t \t return rb; \t} 它仍然會拋出異常 – dataEnthusiast

+0

不,它仍然沒有工作。 – dataEnthusiast

+0

你可以發佈屬性文件嗎? –

0
public static ResourceBundle getBundle(String configLocation) { 
    ResourceBundle rb = ResourceBundle.getBundle("./resources/"+configLocation); 
    return rb; 
}