黑马程序员技术交流社区

标题: 这是一个异常处理的标准代码 [打印本页]

作者: 孙广浩    时间: 2015-11-14 21:35
标题: 这是一个异常处理的标准代码
  1. package cn.itcast;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;

  7. public class Demo5 {
  8.         public static void main(String[] args) {
  9.                
  10.                 // 创建输入流对象
  11.                 BufferedInputStream bis = null;
  12.                 // 创建输出流对象
  13.                 BufferedOutputStream bos = null;
  14.                 try {
  15.                         bis = new BufferedInputStream(new FileInputStream("copy1.mp3"));
  16.                        
  17.                         bos = new BufferedOutputStream(new FileOutputStream("copy3.mp3"));
  18.                        
  19.                         // 读写操作
  20.                         byte[] bys = new byte[1024];
  21.                         int len = 0;
  22.                         while ((len = bis.read(bys)) != -1) {
  23.                                 bos.write(bys, 0, len);
  24.                         }
  25.                 } catch (IOException e) {
  26.                         e.printStackTrace();
  27.                 } finally {
  28.                         // 释放资源
  29.                         if (bis != null) {
  30.                                 try {
  31.                                         bis.close();
  32.                                 } catch (IOException e) {
  33.                                         e.printStackTrace();
  34.                                 }
  35.                         }
  36.                         if (bos != null) {
  37.                                 try {
  38.                                         bos.close();
  39.                                 } catch (IOException e) {
  40.                                         e.printStackTrace();
  41.                                 }
  42.                         }
  43.                 }
  44.         }
  45. }
复制代码

作者: oup    时间: 2015-11-14 21:40
暂时看不懂,就学了一点点




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2