使用Groovy从属性文件获取值
发布时间:2020-12-14 16:38:30 所属栏目:大数据 来源:网络整理
导读:如何使用Groovy从属性文件获取值? 我需要一个属性文件(.properties),它将文件名作为键,并将其目标路径作为值。我将需要在运行时解决的密钥,具体取决于需要移动的文件。 到目前为止,我可以加载属性看起来,但不能从“加载”属性“获
如何使用Groovy从属性文件获取值?
我需要一个属性文件(.properties),它将文件名作为键,并将其目标路径作为值。我将需要在运行时解决的密钥,具体取决于需要移动的文件。 到目前为止,我可以加载属性看起来,但不能从“加载”属性“获取”值。 我提到的线程:groovy: How to access to properties file?和以下是我迄今为止的代码片段 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’)查找,则返回正确的值 但是一旦切换到使用变量“key”,在config [key]中返回 – > [:] 我是新来的,不能说我在这里错过了什么。 解决方法
对我来说,你使事情变得太复杂了。
这是一个简单的例子,应该做这个工作: 给定test.properties文件: a=1 b=2 这段代码运行正常: Properties properties = new Properties() File propertiesFile = new File('test.properties') propertiesFile.withInputStream { properties.load(it) } def runtimeString = 'a' assert properties."$runtimeString" == '1' assert properties.b == '2' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |