【发布时间】:2016-04-27 21:56:24
【问题描述】:
我正在寻找特定的方式来读取文件并将其内容存储为不同的变量。我的属性文件示例:
#Local credentials:
postgresql.url = xxxx.xxxx.xxxx
postgresql.username = xxxxxxx
postgresql.password = xxxxxxx
console.url = xxxxx.xxxx.xxx
目前我正在使用这个 java 代码来读取文件并使用变量:
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("config.properties");
prop.load(input);
this.postgresqlUser = prop.getProperty("postgresql.username")
this.postgresqlPass = prop.getProperty("postgresql.password")
this.postgresqlUrl = prop.getProperty("postgresql.url")
this.consoleUrl = prop.getProperty("console.url")
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
}
}
}
}
我的同事建议使用 groovy 方法来处理这个问题并提到了流,但我似乎找不到太多关于如何将数据存储在单独变量中的信息,目前我所知道的是 def text = new FileInputStream("config.properties").getText("UTF-8") 可以读取整个文件并将其存储在一个变量中,但不能分开。任何帮助将不胜感激
【问题讨论】:
-
@tim 谢谢,解决了我的问题,而不是所有这些尝试和捕获现在我可以使用
propertiesFile.withInputStream闭包。