黑马程序员技术交流社区

标题: IO流异常处理; [打印本页]

作者: ℃葫芦    时间: 2015-8-15 19:30
标题: IO流异常处理;
  1. package cn.itcast.p2.io.filewriter;

  2. import java.io.FileWriter;
  3. import java.io.IOException;

  4. public class IOExceptionDemo {

  5.         private static final String LINE_SEPARATOR = System
  6.                         .getProperty("line.separator");

  7.         /**
  8.          * @param args
  9.          * @throws IOException
  10.          */
  11.         public static void main(String[] args) {

  12.                 FileWriter fw = null;//变量定义在外边
  13.                 try {

  14.                         fw = new FileWriter("k:\\demo.txt");

  15.                         fw.write("abcde" + LINE_SEPARATOR + "hahaha");

  16.                 } catch (IOException e) {
  17.                         System.out.println(e.toString());
  18.                 } finally {
  19.                         if (fw != null)
  20.                                 try {
  21.                                         fw.close();//需要重新try,catch
  22.                                 } catch (IOException e) {
  23.                                         // code....
  24.                                         throw new RuntimeException("关闭失败");
  25.                                 }
  26.                 }

  27.         }

  28. }
复制代码







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