A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 飞龙传神 中级黑马   /  2015-8-16 21:28  /  204 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

字节输出流异常处理案例:
    /**
     * IO流中异常处理方法:
     */
    public class IoExceptionDemo {
     public static void main(String[] args) {
      FileOutputStream fos = null; // 扩大fos的生命周期
      try {
       // 创建字节输出流对象
       fos = new FileOutputStream("IO.txt"); // FileNotFoundException
       // 写入数据
       fos.write("nihao".getBytes()); // IOException
      } catch (IOException e) {
       e.printStackTrace();
      } finally {
       if (fos != null) { // 若fos为null, 不能调用close()
        // 释放资源
        try {
         fos.close(); //IOException
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
      }
     }
    }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马