Are the resources closed at the end regardless of where they are located?
No, only the resources that are declared/defined in the try-with-resources clause are closed at the end of the try block automatically. If you create a resource instead a try block (instead of the try clause), that resource will not be closed automatically. In this code. bw is created in the try clause and so bw.close() will be invoked at the end of the try block automatically.
It is a different matter that there is an explicit call to bw.close in the try block also. This will be executed in the normal course of execution anyway.
If you like our products and services, please help us by posting your review here.