Java 8:需要<identifier>

时间:2019-06-21 05:23:07

标签: java identifier

StringBuilder result = new StringBuilder();
                HttpServletResponse resp = (HttpServletResponse)response;
                int status = 0;
                BufferedReader rd=null;
                CloseableHttpResponse closeableResponse = callApi(request);
                status= closeableResponse.getStatusLine().getStatusCode();
                logger.info("Response Code : {}", status);
                try(
                        rd = new BufferedReader(
                                        new InputStreamReader(closeableResponse.getEntity().getContent()))
                        ){

                        String line = "";
                        while ((line = rd.readLine()) != null) {
                                result.append(line);
                        }

                } finally{
                        if (rd!=null) rd.close();
                }

我在下面的行中会得到预期的标识符:

rd =新的BufferedReader(                                         新的InputStreamReader(closeableResponse.getEntity()。getContent()))                         )

对此有任何想法吗?我已经定义了所有变量。为什么会出现此错误?

1 个答案:

答案 0 :(得分:3)

使用try-with-resources要求您在try部分中声明变量。

类似:

try(BufferedReader rd = getBufferedReader()){
    dfadl
}catch(IOException e){
    dsfsd
}