【发布时间】:2014-01-19 05:19:17
【问题描述】:
如何使用 Groovy 从属性文件中获取值?
我需要一个属性文件 (.properties),它以文件名作为键,以它们的目标路径作为值。我需要在运行时解析密钥,具体取决于需要移动的文件。
到目前为止,我能够加载看起来的属性,但无法从加载的属性中“获取”值。
我提到了线程:groovy: How to access to properties file? 以下是我到目前为止的代码 sn-p
def props = new Properties();
File propFile =
new File('D:/XX/XX_Batch/XX_BATCH_COMMON/src/main/resources/patchFiles.properties')
props.load(propFile.newDataInputStream())
def config = new ConfigSlurper().parse(props)
def ant = new AntBuilder()
def list = ant.fileScanner {
fileset(dir:getSrcPath()) {
include(name:"**/*")
}
}
for (f in list) {
def key = f.name
println(props)
println(config[key])
println(config)
def destn = new File(config['a'])
}
属性文件现在有以下条目:
jan-feb-mar.jsp=/XX/Test/1
XX-1.0.0-SNAPSHOT.jar=/XX/Test/1
a=b
c=d
如果我使用 props.getProperty('a') 查找,则返回正确的值 要么, 配置['a'] 还尝试了代码:notation
但是一旦切换到使用变量“key”,就像在 config[key] 中一样,它会返回 --> [:]
我是 groovy 的新手,不能说我在这里缺少什么。
【问题讨论】:
-
提示:没有像“Java”属性文件这样的东西。它要么是属性文件,要么不是。使用哪种语言(或文本编辑器)编写它并不重要。
-
@tim_yates 谢谢。我见过那个线程。不是这样的:(