2014-12-04 106 views
0

我有,我有這個代碼的至少四個事件(和計數)一個Groovy應用:全局配置文件寫入編碼

new File(filePath).withWriter('UTF-8') { out -> 
    [...] 
} 

覺得應該有規定,我想辦法使用UTF-8每文件編寫,而不必自己指定它。

我可以嗎?怎麼樣?

回答

0

您可以添加一個幫手(如果沒人來了一個正確的答案):

def withUtf8Writer = { file, closure -> new File(file).withWriter('UTF-8', closure) } 

new File('/tmp/f1').withWriter('UTF-8') { out -> 
    out.println 'ẗ' 
} 

withUtf8Writer('/tmp/f2') { out -> 
    out.println 'ẗ' 
} 

assert "diff /tmp/f1 /tmp/f2".execute().text == ''