如何从 try-with-resource 语句关闭资源

问题描述 投票:0回答:1

我编写了以下代码并且运行良好。

  public void  callingTheAPI() throws IOException{

    BufferedReader bufferedReader= null;
    CloseableHttpClient httpclient= null;
    CloseableHttpResponse httpResponse= null;

    try {
        httpclient = HttpClients.createDefault();
        HttpGet httpGet= new HttpGet(URL);
        httpGet.addHeader("Authorization","Bearer "+getToken());

        httpResponse=httpclient.execute(httpGet);
        int responseCode= httpResponse.getStatusLine().getStatusCode();
        System.out.println(responseCode);

        bufferedReader= new BufferedReader(new InputStreamReader (httpResponse.getEntity().getContent()));

        StringBuilder stringBuilder= new StringBuilder();
        String line;
        while((line=bufferedReader.readLine())!=null){
            stringBuilder.append(line);

        }

        System.out.println(stringBuilder.toString());
    } 

    catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    finally{

        if (bufferedReader != null){
            bufferedReader.close();
        }

        if(httpResponse != null){
            httpResponse.close();
        }
        if (httpclient != null){
            httpclient.close();
        }
    }

}

最近我发现从

try-with-resource
开始有一个名为
java 7
的新功能,可以在语句正确运行后自动关闭资源。如何将以下代码转换为使用
try-with-resource
语句。我做了一点,但我对如何向其中添加所有资源感到困惑。这是代码

public void closeResources(){



    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet= new HttpGet(URL);
    httpGet.addHeader("Authorization","Bearer "+getToken());

    CloseableHttpResponse httpResponse=httpclient.execute(httpGet);
    int responseCode= httpResponse.getStatusLine().getStatusCode();
    System.out.println(responseCode);


    try(BufferedReader bufferedReader= new BufferedReader(new InputStreamReader (httpResponse.getEntity().getContent()))){
        StringBuilder stringBuilder= new StringBuilder();
        String line;
        while((line=bufferedReader.readLine())!=null){
            stringBuilder.append(line);

        }

        System.out.println(stringBuilder.toString()); 


    } catch (UnsupportedOperationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }




}

}

您能帮我转换一下这段代码,以便它使用

try-with-resources
语句吗?预先感谢

java try-catch apache-commons-httpclient try-with-resources
1个回答
0
投票

使用“

;
”作为分隔符来分隔资源。现在资源将按照其声明的相反顺序关闭,因此:BufferedReader, 输入流读取器,CloseableHttpResponse。

您还可以从方法返回可关闭的对象,如下例所示,这样您就可以在分配给 try-catch 块之前修改它们。

您可以查看本教程来了解多个 try-catch-resources

 public void closeResources(){
       try(CloseableHttpResponse httpResponse = createCloseableHttpResponse();
        InputStreamReader reader = new InputStreamReader(httpResponse.getEntity().getContent());
        BufferedReader bufferedReader= new BufferedReader(reader)){

        StringBuilder stringBuilder= new StringBuilder();
        String line;
        while((line=bufferedReader.readLine())!=null){
            stringBuilder.append(line);

        }

        System.out.println(stringBuilder.toString());


    } catch (UnsupportedOperationException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private CloseableHttpResponse createCloseableHttpResponse() throws IOException{
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet= new HttpGet("");
    httpGet.addHeader("Authorization","Bearer "+getToken());
    return httpclient.execute(httpGet);
}
© www.soinside.com 2019 - 2024. All rights reserved.