61

I'm a new to Groovy. I used to use 'try-with-resources' construction in my Java code during work with I/O streams.

Could you please advise, is there any analogue of such construction in Groovy?

XZen
  • 225
  • 5
  • 27
  • 49

3 Answers3

70

Groovy 2.3 also has withCloseable which will work on anything that implements Closeable

Groovy 3 newsflash

And Groovy 3+ supports try..with..resources as Java does

https://groovy-lang.org/releasenotes/groovy-3.0.html#_arm_try_with_resources

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • I remember Luke(alkemist)'s tweet looking for this functionality. :) – dmahapatro Apr 30 '14 at 14:03
  • 1
    @dmahapatro Yeah, unfortunately, it's going to clash horribly [with my version](https://github.com/timyates/groovy-common-extensions#withclosable), so I'm going to have to do some renaming I reckon... – tim_yates Apr 30 '14 at 14:26
  • 1
    I can see a difference with the naming. :) `withClosable` vs `withCloseable` – dmahapatro Apr 30 '14 at 14:35
  • @dmahapatro Yay! Less work for me! Hooray for ambiguous spelling ;-) – tim_yates Apr 30 '14 at 14:43
  • Looks like the NIO method is now deprecated in favor of the IO [method](http://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/runtime/IOGroovyMethods.html#withCloseable(U,%20groovy.lang.Closure)). – jaco0646 Sep 30 '15 at 21:33
  • That won't alter its usage though, as it isn't generally called directly – tim_yates Sep 30 '15 at 22:10
  • 3
    Would you add an example of how this method is used with a custom Closeable object? – jaco0646 Oct 01 '15 at 03:00
  • 5
    @jaco0646 - example: `new Socket().withCloseable { socket -> ... }` – Nick Grealy May 11 '16 at 06:51
  • 3
    What if you have an AutoCloseable? Out of luck? – Gregor Petrin May 19 '16 at 07:55
  • 1
    https://github.com/groovy/groovy-core/blob/master/subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java#L1754 says this is `@Deprecated`. Suggests to use https://github.com/groovy/groovy-core/blob/01309f9d4be34ddf93c4a9943b5a97843bff6181/src/main/org/codehaus/groovy/runtime/IOGroovyMethods.java#L1620 instead. – 0x89 Feb 15 '17 at 15:02
  • 1
    @0x89 that's just if you're calling the method statically... Which we're not. When the method is injected into the metaclass of your Closable it will pick the latest one... The deprecation is there for people building groovy tooling, not standard Groovy users – tim_yates Feb 15 '17 at 15:36
  • "that's just if you're calling the method statically... Which we're not" - what do you mean by "that" - the first link or the second? If you mean the first link does not apply, I don't understand why you were linking to it in the first place. – 0x89 Feb 16 '17 at 15:48
  • 2
    It seems to me that there's no example on the internet of how this `withClosable` works... is there any documentation or anything on it? Most importantly, how do I use it with a `java.sql.Connection` or `java.sql.Statement` (not my choice - a Java class is returning them to me.) – ArtOfWarfare Sep 05 '17 at 16:01
  • 1
    @GregorPetrin that was fixed in 2.5.0 (currently in beta): Implement withCloseable on AutoCloseable (https://issues.apache.org/jira/browse/GROOVY-8251) – Boris Lopez Feb 18 '18 at 10:22
  • 1
    Please give example ```withCloseable``` code for opening + writing a File to illustrate the usage, as it stands it's just a time-waster reading through the comments & trying to figure this out. – Ed Randall Dec 01 '18 at 10:07
  • 1
    You wouldn't use this for opening and writing to a file, you'd use `File.withWriter` or `File.withOututputStream`. apologies for wasting your time – tim_yates Dec 01 '18 at 10:39
  • @tim_yates I think it can still be useful in some cases, e. g. for `ZipFile`, which can't be used with `withWriter` nor `withOutputStream` (as it merely represents the ZIPs directory) but still must be closed. It provides `AutoCloseable` interface, so it can be used with `withCloseable`. I think this would be a good example. – zett42 May 12 '20 at 12:04
32

Have a look at the docs on Groovy IO and the associated javadoc. It presents the withStream, withWriter, withReader constructions which are means of getting streams with auto-closeability

Grooveek
  • 10,046
  • 1
  • 27
  • 37
10

Simplest try-with-resources for all Groovy versions is the following (even works with AutoCloseable interface). Where class Thing is a closeable class or implements AutoCloseable.

new Thing().with { res ->
    try {
        // do stuff with res here
    } finally {
        res.close()
    }
}

Which is the equivalent in later versions of Groovy doing:

new Thing().withCloseable { res ->
    // do stuff with res here
}
Sam Gleske
  • 950
  • 7
  • 19