IO流的异常处理

public static void main(String[] args) {
        // try外面创建变量,提升变量的作用域
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("E:\\迅雷下载");
            fos.write("我有一只小毛驴,我从来也不骑。".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("文件写入失败,重试");
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                throw new RuntimeException("关闭资源失败");
            }
        }
    }

 

posted @ 2017-11-19 00:17  小蘇  阅读(275)  评论(0编辑  收藏  举报