我正在尝试从文件位置读取属性文件。但是,资源包无法找到该文件并抛出以下异常
我尝试将文件保留在类路径下,但没有成功读取该文件。但是,我的目的是从文件路径读取属性文件,这来自用户。所以,我的班级可以读取不可知的文件输入来获取属性。
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);
}
}
答案 0 :(得分:1)
您确定以下是
File f = new File("/data/data/" + context.getPackageName() + "/" + fileName);
f.delete();
我认为应该是
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;
}
答案 1 :(得分:0)
public static ResourceBundle getBundle(String configLocation) {
ResourceBundle rb = ResourceBundle.getBundle("./resources/"+configLocation);
return rb;
}