Skip to content

Instantly share code, notes, and snippets.

@koji-k
Last active December 11, 2015 18:08
Show Gist options
  • Save koji-k/4638993 to your computer and use it in GitHub Desktop.
Save koji-k/4638993 to your computer and use it in GitHub Desktop.
def fileName = "/tmp/groovy_io_sample.txt"
def encode = "UTF-8"
def file = new File(fileName)
// 存在確認
if (file.exists()) {
println "not exists"
} else {
println "not exists"
}
// write
file.withWriterAppend(encode) {it << "ABCDEFG"}
file.append("HIJKLMN",encode)
// read pattern 1
file = new File(fileName)
file.eachLine(encode){println it}
// read pattern 2
file = new File(fileName)
println file.getText(encode)
// read pattern 3
file = new File(fileName)
println file.newReader(encode).text
// ファイルの削除
file.delete()
// ファイルが削除されている事を確認
assert file.exists() == false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment