From 572c6ddedd41358d6484c79cb566128e96757b65 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Tue, 11 Aug 2026 13:53:42 +0800 Subject: [PATCH] =?UTF-8?q?s-day09-JDK7=E4=B9=8B=E5=89=8DIO=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E7=9A=84=E5=A4=84=E7=90=86=E6=96=B9=E5=BC=8F(?= =?UTF-8?q?=E9=87=8D=E7=82=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/inmind/io_exception05/Demo13.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 s-day09/src/com/inmind/io_exception05/Demo13.java 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; + } + + } + } +}