Tuesday, June 9, 2009

Using Groovy to pad a file

The other day someone asked me if there was a quick way to right-pad lines in a file with spaces to a certain length. I couldn't find a setting to do this in my favorite text editor Notepad++, so I thought I'd see what it would take to do in Groovy.

Not a very exciting script, but I truly admire how much you can do with Groovy in just a few lines of code.

def outfile = new File("out.txt")
def infile = new File("in.txt")

outfile.withWriter { output ->
infile.eachLine { input ->
output.writeLine(input.padRight(200, " "))

No comments: