Whe I saw this code in its answer of course I replied back "Why didn't you use try-with-resource in previous code you wrote?" (That code is the one we used until Java 7 then came try-with-resources and I expect ChatGPT gave me this solution.) And I got the code I want.