A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 薛鹏鹏 高级黑马   /  2013-8-7 20:20  /  1586 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


如图,不抛的话,怎么try
  1. public static void copy_1()throws IOException  //这里不想抛异常的话,怎么处理
  2.         {
  3.                 BufferedInputStream bufis = new BufferedInputStream(
  4. new FileInputStream("Date01.java"));
  5.                 BufferedOutputStream bufos = new BufferedOutputStream(
  6. new FileOutputStream("Date00.java"));

  7.                 int by = 0;

  8.                 while((by=bufis.read())!=-1)
  9.                 {
  10.                         bufos.write(by);
  11.                 }

  12.                         bufos.close();
  13.                         bufis.close();
  14.         }
复制代码

评分

参与人数 1技术分 +1 收起 理由
Sword + 1

查看全部评分

3 个回复

倒序浏览
  1. public static void copy_1()
  2. {
  3.                 BufferedInputStream bufis = null;
  4.                 BufferedOutputStream bufos = null;
  5.                 try
  6.                 {
  7.                         bufis = new BufferedInputStream(new FileInputStream("Demo.java"));
  8.                         bufos = new BufferedOutputStream(new FileOutputStream("Demo11.java"));
  9.             int by = 0;
  10.             while((by=bufis.read())!=-1)
  11.             {
  12.                   bufos.write(by);
  13.             }
  14.                 }
  15.                 catch (IOException e)
  16.                 {
  17.                         throw new RuntimeException("读取异常");
  18.                 }
  19.                 finally{
  20.                 if(bufis!=null)
  21.                         try
  22.                         {
  23.                                 bufis.close();
  24.                         }
  25.                         catch (IOException e)
  26.                         {
  27.                                 System.out.println("关闭资源异常");
  28.                         }
  29.                 if(bufos!=null)
  30.                         try
  31.                         {
  32.                                 bufos.close();
  33.                         }
  34.                         catch (IOException e)
  35.                         {
  36.                                 System.out.println("关闭资源异常");
  37.                         }
  38.                 }
  39. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
Sword + 1

查看全部评分

回复 使用道具 举报
  1.         public static void copy_1() { // 这里不想抛异常的话,怎么处理
  2.                 try (BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("Date01.java"));
  3.                                 BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("Date00.java"));) {
  4.                         int by = 0;
  5.                         while ((by = bufis.read()) != -1) {
  6.                                 bufos.write(by);
  7.                         }
  8.                 } catch (IOException e) {
  9.                         e.printStackTrace();
  10.                 }
  11.         }
复制代码

评分

参与人数 1技术分 +1 收起 理由
Sword + 1

查看全部评分

回复 使用道具 举报
如果不进行总体抛出异常的话,那就要一步步的分别出现异常的流进行try{}catch{}和finally{}操作,先进行初始化,在进行初始化时一抛出异常,对象初始化就失败,对象就不存在。所以对之进行try{}catch{}异常处理,记住在进行finally操作的时候一定要对关闭的流进行不为空的判断。以下是我的编写:
  1. public class Copy {
  2.         public static void copy_1() {
  3.                 // 初始化一抛出异常,对象初始化就失败,对象就不存在。
  4.                 BufferedInputStream bufis = null;
  5.                 BufferedOutputStream bufos = null;
  6.                 try {
  7.                         bufis = new BufferedInputStream(new FileInputStream("Demo.java"));
  8.                         bufos = new BufferedOutputStream(new FileOutputStream("Demo11.java"));
  9.                         int by = 0;
  10.                         while ((by = bufis.read()) != -1) {
  11.                                 bufos.write(by);//调用write方法,将字符串写入到流中()。
  12.                         }
  13.                 }
  14.                 catch (IOException e) {
  15.                         throw new RuntimeException("读取文件失败");
  16.                 }
  17.                 finally {
  18.                                 try {
  19.                                         //finally中一定要对关闭的流进行不为空的判断
  20.                                         if (bufis != null)
  21.                                         bufis.close();
  22.                                 } catch (IOException e) {
  23.                                         System.out.println("读取关闭失败");
  24.                                 }
  25.                                 try {
  26.                                         if (bufos != null)
  27.                                         bufos.close();
  28.                                 } catch (IOException e) {
  29.                                         System.out.println("写入关闭失败");
  30.                                 }
  31.                 }
  32.         }
  33.         public static void main(String[] args) {
  34.                 copy_1();
  35.         }
  36. }
复制代码
希望互助学习!

评分

参与人数 1技术分 +1 黑马币 +3 收起 理由
Sword + 1 + 3 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马