2012-10-05

Groovy File One Liners

Groovy Scripting and Programming: Groovy File One Liners

One of the nice things about Groovy if you are a Java programmer is the ease that you can manipulate files with just one like of code.

Create or replace a file and write a line of text to it:
new File("myfile.txt").write("Here is a line: \"whats a nice programmer like you doing at a blog like this\" \n");

Append to a file (creates the if it doesn't exist)
new File("myfile.txt").append("\"Looking for a some code like this\"\n");

Read the second line (First line is line 0) of the file
println new File("myfile.txt").readLines()[1] //what was the response

No comments: