黑马程序员技术交流社区

标题: 字节输出流异常处理案例: [打印本页]

作者: 飞龙传神    时间: 2015-8-16 21:28
标题: 字节输出流异常处理案例:
字节输出流异常处理案例:
    /**
     * 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();
        }
       }
      }
     }
    }





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2