- package cn.itcast.p2.io.filewriter;
- import java.io.FileWriter;
- import java.io.IOException;
- public class IOExceptionDemo {
- private static final String LINE_SEPARATOR = System
- .getProperty("line.separator");
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) {
- FileWriter fw = null;//变量定义在外边
- try {
- fw = new FileWriter("k:\\demo.txt");
- fw.write("abcde" + LINE_SEPARATOR + "hahaha");
- } catch (IOException e) {
- System.out.println(e.toString());
- } finally {
- if (fw != null)
- try {
- fw.close();//需要重新try,catch
- } catch (IOException e) {
- // code....
- throw new RuntimeException("关闭失败");
- }
- }
- }
- }
复制代码
|
|