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

© ℃葫芦 中级黑马   /  2015-8-15 19:30  /  285 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  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. }
复制代码


0 个回复

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