2016-04-21 26 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

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

相關問題