Java resource closing

后端 未结 8 1447
一生所求
一生所求 2021-01-12 00:57

I\'m writing an app that connect to a website and read one line from it. I do it like this:

try{
        URLConnection connection = new URL(\"www.example.com         


        
8条回答
  •  鱼传尺愫
    2021-01-12 01:17

    I think it will be better to place the closing methods in the finally block

    Yes, always. Because an exception might occur and resources aren't released/closed properly.

    You only need to close the most outer reader because it will be responsible for closing any enclosing readers.

    Yes, it's ugly... for now. I think there are plans for an automatic resource management in Java.

提交回复
热议问题