Java resource closing

后端 未结 8 1663
一生所求
一生所求 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:27

    In scope of Java 8 I would use alike:

    try(Resource resource = acquire()) {
        use(resource);
        reuse(resource);
    }
    

提交回复
热议问题