zoukankan      html  css  js  c++  java
  • java io读书笔记(7) Closing Output Streams

    输出完毕后,需要close这个stream,从而使操作系统释放相关的资源。举例:

    public void close( ) throws IOException

    并不是所有的stream都需要close,可是,诸如file或者network,打开后,需要关闭。

    try {
      OutputStream out = new FileOutputStream("numbers.dat");
      // Write to the stream...
      out.close( );
    }
    catch (IOException ex) {
      System.err.println(ex);
    }

    However, this code fragment has a potential leak. If an IOException is thrown while writing, the stream won't be closed. It's more reliable to close the stream in a finally block so that it's closed whether or not an exception is thrown. To do this you need to declare the OutputStream variable outside the try block. For example:

    // Initialize this to null to keep the compiler from complaining
    // about uninitialized variables
    OutputStream out = null;
    try {
      out = new FileOutputStream("numbers.dat");
      // Write to the stream...
    }
    catch (IOException ex) {
      System.err.println(ex);
    }
    finally {
      if (out != null) {
        try {
          out.close( );
        }
        catch (IOException ex) {
          System.err.println(ex);
        }
      }
    }
  • 相关阅读:
    红蓝对抗
    SQLMAP用法大全
    Web安全工程师(进阶)课程表
    msf连接PostgreSQL数据库
    我的web安全工程师学习之路——规划篇
    web安全深度剖析pdf
    js面试题
    js克隆一个对象
    js面试必考:this
    前端面试:js数据类型
  • 原文地址:https://www.cnblogs.com/aomi/p/3192852.html
Copyright © 2011-2022 走看看