AutoTig
AutoTig

Reputation: 105

Writing a groovy script results into a file

I would like to write groovy script results in to a file on my Mac machine. How do I do that in Groovy? Here my attempt:

log.info "Number of nodes:" + numElements
log.info ("Matching codes:"+matches)
log.info ("Fails:"+fails)

// Exporting results to a file
today = new Date()
sdf = new java.text.SimpleDateFormat("dd-MM-yyyy-hh-mm")
todayStr = sdf.format(today)
new File( "User/documents" + todayStr + "report.txt" ).write(numElements, "UTF-8" )
new File( "User/documents" + todayStr + "report.txt" ).write(matches, "UTF-8" )
new File( "User/documents" + todayStr + "report.txt" ).write(fails, "UTF-8" )

Can anyone help in the exporting results part?

Thanks, Regards, A ok, i've managed to create a file with

file = new File(dir + "${todayStr}_report.txt").createNewFile()

How do I add the numelements,matches and fails? like this:

 File.append (file, matches)? 

I get the follwoing error:

groovy.lang.MissingMethodException: No signature of method: static java.io.File.append() is applicable for argument types: (java.lang.Boolean, java.util.ArrayList) values: [true, [EU 4G FLAT L(CORPORATE) SP, EU 4G FLAT M(CORPORATE), ...]] Possible solutions: append(java.lang.Object, java.lang.String), append(java.io.InputStream), append(java.lang.Object), append([B), canRead(), find() error at line: 33

Upvotes: 2

Views: 7589

Answers (1)

Mateusz Chrzaszcz
Mateusz Chrzaszcz

Reputation: 1280

You have wrong filepath. I don't have MAC so I'm not 100% sure but from my point of view you should have:

new File("/User/documents/${todayStr}report.txt").write(numElements, "UTF-8")

You lack at least two backslashes, first before User and second after documents in your path. With the approach you have now, it tries to save to a directory User/documentDATE, pretty sure it does not exist.

So above I showed you way with absolute path. You can also write strictly like this:

 new File("${todayStr}report.txt").write(numElements, "UTF-8")

and if the file is created then you'll be 100% sure it is a problem with your filepath :)

A few more things - since it is a groovy, try to use the advantages that language has over Java, there are several ways of working with files, I've also rewritten your logs to show you how simple it is to work with Strings in groovy:

log.info "Number of nodes:  ${numElements}"
log.info "Matching codes: ${matches}"
log.info "Fails: ${fails}"

I hope it helps

Upvotes: 2

Related Questions