Heim  >  Fragen und Antworten  >  Hauptteil

关于java中流关闭的问题

有如下代码:

private static String extractContent(HttpResponse response) throws Exception {
    String htmStr = null;
    if (response.getStatusLine().getStatusCode() == 200) {
        if (response != null) {
            BufferedReader br = null;
            InputStream ins = null;
            try {
                HttpEntity entity = response.getEntity();
                ins = entity.getContent();
                br = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
                StringBuffer sbf = new StringBuffer();
                String line = null;
                while ((line = br.readLine()) != null) {
                    sbf.append(line);
                }
                // 处理内容
                htmStr = sbf.toString();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != br) {
                    br.close();
                }
                if (null != ins) {
                    ins.close();
                }
            }
        }
    }
    return htmStr;
}

代码中的br 和ins都正确的关闭掉了。但是在使用InputStreamReader的时候,是通过
br = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
来使用的,那么InputStreamReader是否需要显示的关闭,即改成:

private static String extractContent(HttpResponse response) throws Exception {
    String htmStr = null;
    if (response.getStatusLine().getStatusCode() == 200) {
        if (response != null) {
            BufferedReader br = null;
            InputStream ins = null;
            InputStreamReader inr = null;
            try {
                HttpEntity entity = response.getEntity();
                ins = entity.getContent();
                inr = new InputStreamReader(ins, "UTF-8");
                br = new BufferedReader(inr);
                StringBuffer sbf = new StringBuffer();
                String line = null;
                while ((line = br.readLine()) != null) {
                    sbf.append(line);
                }
                // 处理内容
                htmStr = sbf.toString();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != br) {
                    br.close();
                }
                if (null != ins) {
                    ins.close();
                }
                if (null != inr) {
                    inr.close();
                }
            }
        }
    }
    return htmStr;
}

我知道在JDK7之后提供了try with resources的方法解决了这种问题,但是在JDK7之前是不是必须的这样用,显示的分别new相应流对象,然后最后再关闭,而不能
br = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
像这样嵌套这来用。
我的问题:
1.如果没有流关闭,会带来安全隐患,具体带来何种安全隐患?最好能举例说明。还有说没有正确的关闭流,会造成系统资源的浪费,具体是指什么?(我的理解是浪费CPU资源,导致CPU轮询去查询是否有I/O传输,这样的理解对不对啊)??
2.jdk1.7之前,流的使用,是不是必须要用这种结构:

BufferedReader ins = null;
InputStreamReader inr = null;
    try{
        ins = ...
        inr = ...
    }catch{
    ...
    }finally{
        if(null != ins) ins.close;
        if(null != inr) inr.close;
    }

而不能使用这样的结构:

BufferedReader ins = null;
    try{
        ins = new InputStreamReader(new InputStrea(System.in, "UTF-8"));
    }catch{
    ...
    }finally{
        if(null != ins) ins.close;
    }

黄舟黄舟2715 Tage vor384

Antworte allen(2)Ich werde antworten

  • 高洛峰

    高洛峰2017-04-18 09:32:13

    https://segmentfault.com/q/1010000005970993?_ea=969143

    Antwort
    0
  • 迷茫

    迷茫2017-04-18 09:32:13

    1. 如果没有关闭流:

      数据可能还在缓冲区,没有flush。
      文件可能还被系统lock,没有释放。
      。。。(待补充)
    2. 包装流的close方法被调用时会自动调用被包装流的close。(可以去jdk源码里求证一下)

    Antwort
    0
  • StornierenAntwort