Groovy: temporary file with autodelete
examples/groovy/temp_file.gvy
File.createTempFile("temp",".tmp").with { //deleteOnExit() // Including this line will cause the temp file to be deleted automatically write "Hello world" // write to the temp file println absolutePath // This is the path to the file }
examples/groovy/temp_file_oop.gvy
File tf = File.createTempFile("temp",".tmp") tf.write("Hello world") // write to the file println(tf.absolutePath) // path to the file tf.delete() // delete the file
Comments
Thank you very much, your answer resolved my issue:
def map1 = [:] map1.put('jfr_1', "Hello World!") def i = 1 def x = map1."jfr_$i" println "x = " + "${x}" ----> x = Hello World!
def jfr_1 = "Hello World!" def i = 1 def x = "jfr_$i" println "${x}"
Above code prints: jfr_1 instead of Hello World! What I'm doing wrong?
---
Nothing. That's the expected behaviour.
Thank you for the reply, how can I print "Hello World!" instead Using PERL print ($$x) will print "Hello World!".
---
Even in Perl you should not do that and you should "use strict" to make sure you don't do it by mistake. In Perl you'd use a hash in Groovy a map for that kind of data structure.
Published on 2018-10-30