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("关闭资源失败"); } } }