2016-10-04 155 views
-2

在Groovy腳本中,如何讀取給定文件夾中的所有.txt文件,並通過複製.txt文件的內容來創建單個文檔文檔。Groovy腳本讀取所有.txt文件並寫入單個文檔

示例方案 -

在目錄C:\樣本I具有n沒有的.txt文件。我需要複製.txt文件的數據並創建一個.doc文件並複製粘貼所有.txt文件的內容。

請讓我知道如何在Groovy腳本

+0

請讓我們知道你已經嘗試過至今 – injecteer

+0

我是新來的Groovy腳本我不知道如何着手。 – Vinu

回答

0

做到這一點戳互聯網有點後,我能想出下面的腳本:

@Grab(group='org.apache.poi', module='poi-ooxml', version='3.7') 
import org.apache.poi.xwpf.usermodel.XWPFDocument 

XWPFDocument doc = new XWPFDocument() 

new File('/some/dir').eachFile{ File f -> 
    if(f.directory) return 
    doc.createParagraph().createRun().text = "--------- $f.name ---------" 
    doc.createParagraph().createRun().text = f.text 
} 

new File('result.docx').withOutputStream{ doc.write it } 

所產生的docx文件是以下的「結構」:

------------- error.gsp ------------- 
<!doctype html> 
<html> 
    <head> 
.... 
------------- index.gsp ------------- 
<!doctype html> 
<html> 
相關問題