diff --git a/s-day09/src/com/inmind/io_exception05/Demo13.java b/s-day09/src/com/inmind/io_exception05/Demo13.java new file mode 100644 index 0000000..1fb7db9 --- /dev/null +++ b/s-day09/src/com/inmind/io_exception05/Demo13.java @@ -0,0 +1,49 @@ +package com.inmind.io_exception05; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/* +JDK7之前IO异常的处理方式(重点) + */ +public class Demo13 { + public static void main(String[] args) { + FileInputStream fis = null; + FileOutputStream fos = null; + try { + //文件复制(边读边写) + fis = new FileInputStream("3.jpg"); + fos = new FileOutputStream("4.jpg"); + + byte[] bytes = new byte[1024]; + int len; + + while ((len = fis.read(bytes)) != -1) { + fos.write(bytes, 0, len); + } + } catch (IOException e) { + e.printStackTrace(); + }finally { + //资源的释放 + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + fis = null;//避免内存泄漏 + } + + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + fos = null; + } + + } + } +}