【问题标题】:How to replace value in text file with integer? - Groovy (Qupath)如何用整数替换文本文件中的值? - Groovy (Qupath)
【发布时间】:2023-01-27 02:14:37
【问题描述】:

我想用文本值中的整数替换特定值。好像我错过了一些重要的东西。

x=5


def file = new File('/Users/path/name.txt')
def newConfig = file.text.replace('abc',x)
file.text = newConfig

试过 x.toString() 但也没有帮助。

我正在使用此代码生成一个数字,并需要在 .json 文件中替换该数字。

谢谢

【问题讨论】:

    标签: groovy replace config


    【解决方案1】:

    假设我们有一个包含以下字符串的文件:

    testabcandanotherabc
    abcandabcandanotherabc
    

    我使用以下代码:

    x = 5
    def file = new File('/Users/path/name.txt')
    def newConfig = file.text.replaceAll('abc', x.toString())
    file.write(newConfig)
    

    它将文件内容更改如下:

    test5andanother5
    5and5andanother5
    

    我希望它会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-18
      • 2012-06-24
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 2017-08-01
      相关资源
      最近更新 更多