2016-04-21 70 views
0
import groovy.io.FileType 
import java.io.File; 
def list = [] 
def dir = new File("C:\\Users\\Desktop\\CodeTest") 

    dir.eachFileRecurse (FileType.FILES) 
     { 
      file ->list << file 
     } 
    list.each 
     { 
      println it.path 
     } 
//Replace the pattern in file and write to file sequentially. 
def replacePatternInFile(file, Closure replaceText) 
    { 
      file.write(replaceText(file.text)) 
    } 
def file = new File(file) 
def patternToFind1 = ~/</ 
def patternToFind2 = ~/>/ 
def patternToReplace1 = '&lt' 
def patternToReplace2 = '&gt' 

//Call the method 
replacePatternInFile(file){ 
    it.replaceAll(patternToFind1,patternToReplace1) 
} 

replacePatternInFile(file){ 
    it.replaceAll(patternToFind2,patternToReplace2) 
} 

println file.getText() 

我能够更改一个文件的模式,但我想读取文件夹中的所有文件并将每个文件中的模式替换为一个通过一个 在执行它: ERROR:发生错误[找不到匹配的构造函数:java.io.File中(java.util.ArrayList中),请参阅错误日志以了解详细如何读取文件夹中的所有文件并使用Groovy替换文件中的模式

回答

0

你有很多问题您代码...

1)您不需要导入:

import java.io.File; 

2)当你调用:

def file = new File(file) 

没有叫new File(file)file(可变您的意思是否files

3)如果你确实意味着new File(files)那么这是你的错误是...您无法从字符串列表中创建新文件

4)>的实体是&gt; NOT &gt ... <(最后需要分号)

您需要遍历字符串列表(files.each { path ->?),然后依次处理每个字符串。

尽管2)和3)让我怀疑上面的代码不是你真正的代码,而是从内存中假装复制(或者是严重编辑的副本),因为上面的代码不会给你你说的错误你越来越

+0

谢谢我做了更改 –

+0

它现在的作品?希望如此:-)祝你好运! –

相关问题