Strictly Programming

Some notes on Groovy

Posted in Uncategorized by sqllyw on 12/04/2008


A few quick notes on the use of Groovy


Read a text file

new File('names.csv').splitEachLine(',') {
	fields ->; println fields[0] +"," + fields[4] + "," + fields[1]
}

save this as csv2txt.groovy, you can run it:

groovy csv2txt

or

groovy csv2txt > names.txt

Write to a text file

new File('names.txt').withPrintWriter { out ->
	new File('names.csv').splitEachLine(',') {
		fields -> out.println fields[0] +"," + fields[4] + "," + fields[1]
	}
}

save this file as csv2txt2.groovy, then run:

groovy csv2txt2

you should see a names.txt file in the same directory.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: