Project Lombok provide an easy way to ensure a given resource is automatically cleaned up before the code execution path exits your current scope. All you need to do is to annotate any local variable declaration with the @Cleanup annotation.
As-Is (Do not annotate @Cleanup)
To-Be(Annotte with @Cleanup)
It will ensure the variable declaration that you annotate will be cleaned up by calling its close method, regardless of what happens. Implemented by wrapping all statements following the local variable declaration to the end of your scope into a try block that, as a finally action, closes the resource.