【发布时间】:2019-10-02 09:05:23
【问题描述】:
我需要询问如何为我的应用程序读取配置文件的最佳方式。 目前我正在做的是将上面的代码放在我的Java程序中,获取我想要的所有配置项,然后将值传递给需要变量的函数。
让配置文件只加载一次并且可以在其他进程(其他用户)中使用的最佳方法是什么?
File configFile = new File("D:\\config.properties");
try {
FileReader reader = new FileReader(configFile);
Properties prop = new Properties();
prop.load(reader);
String dbName = prop.getProperty("dbName");
String dBase = prop.getProperty("database");
String strMethod1 = prop.getProperty("method1");
int method1 = Integer.parseInt(strMethod1);
String strMethod2 = prop.getProperty("method2");
int method2 = Integer.parseInt(strMethod2);
} catch (IOException e) {
returnValue = IO_EXCEPTION;
logger.error("IOException:::" + e + " returnValue:::" + returnValue);
}
【问题讨论】:
-
将值存储在静态变量中或使用单例模式。那将是最简单的方法。并不是说这是最好的方法。
标签: java properties-file