parsing - groovy更新配置文件

标签 parsing properties groovy

我有一个看起来像这样的常规配置文件

section1 {
    prop1 = val1
    prop2 = val2
    section2 {
       prop3 = val3
    {
}

// other style properties in this file
anotherprop = someval

// as well as some other statements
println "hello there"

例如,我想编写一个 groovy 脚本来更改 prop3 的值。在 groovy 中有没有好的方法来做到这一点?这有点困难,因为该文件包含多种样式的属性以及 println。

最佳答案

据我了解,您想修改配置文件

由于文件中有 println 和其他语句,因此无法使用配置 Slurper 读取文件。

我想最简单的方法(但我希望这个解决方案会被否决)-:是使用 regsub:

def config = new File('config.groovy').getText()
def newValue = 18
def newConfig = config.replaceAll(/(?ms)(prop3[ \t]*=[ \t]*)([0-9]*)/,'$1'+newValue)
new File('config.groovy').write(newConfig)

在大多数情况下这应该是相当稳定的......

关于parsing - groovy更新配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12751230/

相关文章:

java - 如何使用Spring在Java中将一个属性使用到另一个属性中?

spring - 在运行时将动态属性应用于 bean

java - 将列表从 Java 返回到 Freemarker

r - 解析不同格式的日期

java - 对元素的 DOM 属性序列进行排序

c# - 在表达式中插入节点/属性

Groovy - 使用集合中的 where 子句计数不同

java - 将 for java 循环转换为 groovy

java - 在 Java 中读取一个 json 文件

java - 如何验证 URL 在 Java 1.6 中是否有效?