如何在groovy中替换文本文件中的字符串/单词 [英] how to replace a string/word in a text file in groovy

查看:292
本文介绍了如何在groovy中替换文本文件中的字符串/单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我正在使用groovy 2.1.5,并且我必须编写一个代码来显示给定路径的目录的文件/文件,然后它将备份文件并从文件中替换一个字/字符串。
这里是我用来替换所选文件中的单词的代码

  String contents = new File ('/geretd/resume.txt').getText('UTF-8')
contents = contents.replaceAll('visa','viva')
pre>

如果有人希望以更有效的方式修改它,这里还有我的完整代码,因为我正在学习,所以我会很感激。

  def dir = new File('/ geretd')
dir.eachFile {
if(it.isFile()){
println it.canonicalPath
}
}

copy = {File src,File dest->

def input = src.newDataInputStream()
def output = dest.newDataOutputStream()

输出<<输入

input.close()
output.close()
}

// File srcFile = new File(args [0])
// File destFile = new File(args [1])$ ​​b
$ b文件srcFile = new File('/ geretd / resume.txt')
File destFile = new File(' /geretd/resumebak.txt')
copy(srcFile,destFile)

x =
println x

def dire = new File(' / geretd')
dir.eachFile {
if(it.isFile()){
println it.canonicalPath
}
}

String contents = new File('/geretd/resume.txt').getText('UTF-8')
contents = contents.replaceAll('visa','viva')


 新文件('destination.txt').withWriter {w  - > 
新文件('source.txt').eachLine {line - >
w<< line.replaceAll('World','World !!!')+ System.getProperty(line.separator)
}
}

当然,这(和 dmahapatro的答案)依赖于你正在更换的字词不是跨越行


Hello I am using groovy 2.1.5 and I have to write a code which show the contens/files of a directory with a given path then it makes a backup of the file and replace a word/string from the file. here is the code I have used to try to replace a word in the file selected

String contents = new File( '/geretd/resume.txt' ).getText( 'UTF-8' ) 
contents = contents.replaceAll( 'visa', 'viva' )

also here is my complete code if anyone would like to modify it in a more efficient way, I will appreciate it since I am learning.

def dir = new File('/geretd')
dir.eachFile { 
    if (it.isFile()) {
        println it.canonicalPath
    }
}

copy = { File src,File dest-> 

    def input = src.newDataInputStream()
    def output = dest.newDataOutputStream()

    output << input 

    input.close()
    output.close()
}

//File srcFile  = new File(args[0])
//File destFile = new File(args[1])

File srcFile  = new File('/geretd/resume.txt')
File destFile = new File('/geretd/resumebak.txt')
copy(srcFile,destFile)

x = " "
println x

def dire = new File('/geretd')
dir.eachFile { 
    if (it.isFile()) {
        println it.canonicalPath
    }
}

String contents = new File( '/geretd/resume.txt' ).getText( 'UTF-8' ) 
contents = contents.replaceAll( 'visa', 'viva' )

解决方案

As an alternative to loading the whole file into memory, you could do each line in turn

new File( 'destination.txt' ).withWriter { w ->
  new File( 'source.txt' ).eachLine { line ->
    w << line.replaceAll( 'World', 'World!!!' ) + System.getProperty("line.separator")
  }
}

Of course this (and dmahapatro's answer) rely on the words you are replacing not spanning across lines

这篇关于如何在groovy中替换文本文件中的字符串/单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆