English 中文(简体)
我如何在格罗莫夫建立临时档案?
原标题:How do I create a temporary file in Groovy?

在 Java,有创建临时档案的java.io.File.createTempFile功能。 在格罗维耶,似乎没有这样的功能,因为这一功能从档案中消失。 (见:)

在格罗莫夫,是否有一条临时档案或档案道路,或者我是否需要建立自己(如果我不错的话,那不会容易获得权利)?

事先感谢你!

最佳回答
File.createTempFile("temp",".tmp").with {
    // Include the line below if you want the file to be automatically deleted when the 
    // JVM exits
    // deleteOnExit()

    write "Hello world"
    println absolutePath
}

Simplified Version

有些人评论说,他们可以列举如何使用上述代码的简单(但功能上相同)版本。

File file = File.createTempFile("temp",".tmp")
// Include the line below if you want the file to be automatically deleted when the 
// JVM exits
// file.deleteOnExit()

file.write "Hello world"
println file.absolutePath
问题回答

您可在格罗夫法典中使用java.io.File.createTempFile()。

def temp = File.createTempFile( temp ,  .txt ) 
temp.write( test )  
println temp.absolutePath

格罗莫夫的班级延长 Java档案班,就像通常在贾瓦邦那样。

File temp = File.createTempFile("temp",".scrap");
temp.write("Hello world")
println temp.getAbsolutePath()  




相关问题
Groovy - how to exit each loop?

I m new to Grails/Groovy and am trying to find a node in a an xml file; I ve figured out how to iterate over all of them, but I want to exit the loop when the target node is found. I ve read that ...

Eclipse Spring Builder set properties with Groovy beans

I typically use groovy to construct simple bean but the Spring IDE plugin to eclipse fails to build when I try to set a property that is generated by groovy without an explicit setter. For example, ...

How can I get this snippet to work?

I d like to port a little piece of code from Ruby to Groovy, and I m stuck at this: def given(array,closure) { closure.delegate = array closure() } given([1,2,3,4]) { findAll { it > 4} ...

Changing the value in a map in Groovy

This is about a very basic program I m writing in Groovy. I have defined a map inside a method: def addItem() { print("Enter the item name: ") def itemName = reader.readLine() print(...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

热门标签