博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(17)IO中的异常处理
阅读量:6999 次
发布时间:2019-06-27

本文共 3937 字,大约阅读时间需要 13 分钟。

public static void copyImage() throws IOException

{

//找到目标文件

File inFile = new File("D:\\1.jpg");

File destFile = new File("E:\\1.jpg");

//建立数据的输入输出通道

FileInputStream fileInputStream = new  FileInputStream(inFile);

FileOutputStream fileOutputStream = new FileOutputStream(destFile); //追加数据....

 

//每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。

//建立缓冲数据,边读边写

byte[] buf = new byte[1024];

int length = 0 ;

while((length = fileInputStream.read(buf))!=-1){ //不限丢人的输出来第一次我写的时候,read(buf)中的参数buf被我写丢了,好久才发现的错误

fileOutputStream.write(buf,0,length); //写出很多次数据,所以就必须要追加。

}

//关闭资源 原则: 先开后关,后开先关。

fileOutputStream.close();

fileInputStream.close();

}

1 //输入字节流的异常处理。 2     public static void readTest() 3     { 4         FileInputStream fileInputStream = null; 5         try 6         { 7             //找到目标文件 8             File file = new File("D:\\b.txt"); 9             //建立数据输入通道10             fileInputStream = new FileInputStream(file);11             //建立缓冲数组,读取数据12             byte[] buf = new byte[1024];13             int length = 0;14             while((length = fileInputStream.read(buf))!=-1)15             {16                 System.out.println(new String(buf, 0, length));17             }18         }catch(IOException e){19             //处理读出异常的代码 首先要阻止后面的代码执行,而且还要通知调用者出错了... throw20 //            throw e;    //我们一般不会这样做,或者上面函数声明处使用throws 这个样子需要强迫调用者 trycatch21             throw new RuntimeException(e);    //将IOException包装一层,然后再抛出,这样子做的目的是为了让调用者更加的方便,运行时异常不需要trycatch包围。22             23         }finally24         {25             //关闭资源26             try27             {28                 if(fileInputStream!=null){    //关闭之前应当先检查,万一之前通道就没有建立成功29                     fileInputStream.close();//这一块想想必须应当放到finally块重,为什么,因为如果碰到了异常,总不能不把资源释放吧30                     System.out.println("关闭资源成功过了");                31                 }32             } catch (IOException e)33             {34                 System.out.println("关闭资源失败");35                 throw new RuntimeException(e);36             }37 38         }39     }40 //拷贝图片是有输入流有输出流 综合在一块做异常处理,值得玩味。41     public static void copyImage()42     {43         FileInputStream fileInputStream = null;44         FileOutputStream fileOutputStream = null;45         //找到目标文件46         try47         {48             File inFile = new File("D:\\1.jpg");49             File outFile = new File("E:\\1.jpg");50             51             //建立输入输出通道52             fileInputStream = new FileInputStream(inFile);53             fileOutputStream = new FileOutputStream(outFile);54             55             //缓冲数组56             byte[] buf = new byte[1024];57             int length = 0;58             while((length = fileInputStream.read(buf))!=-1)59             {60                 fileOutputStream.write(buf, 0, length);61             }62         }catch (IOException e)63         {64             System.out.println("拷贝图片出错。。。");65             throw new RuntimeException(e);66         }finally67         {68             try69             {70                 if(fileOutputStream!=null)71                 {72                     fileOutputStream.close();73                     System.out.println("关闭资源成功。。。");74                 }75             } catch (IOException e)76             {77                 System.out.println("关闭资源失败。。。");78                 throw new RuntimeException(e);79             }finally80             {81                 try82                 {83                     if( fileInputStream!=null )84                     {85                         fileInputStream.close();86                         System.out.println("关闭资源成功...");87                     }88                 } catch (IOException e)89                 {90                     System.out.println("关闭资源失败...");91                     throw new RuntimeException(e);92                 }93             }    94         }    95     }

 

转载于:https://www.cnblogs.com/OliverZhang/p/6022040.html

你可能感兴趣的文章
在JavaScript面向对象编程中使用继承(1)
查看>>
高铁与机场成交通信息化建设的双驾马车
查看>>
chmod命令
查看>>
货币的起源和职能是什么?绘制货币资金管理思维导图简单的方法介绍
查看>>
springboot+kafka+elk+docker+docker-compose+centos搭建日志收集系统
查看>>
时讯无线如何满足商业区的无线覆盖?
查看>>
2014最新open***搭建实例
查看>>
WinAPI: midiOutCachePatches - 预装音色
查看>>
finally执行顺序
查看>>
TWebBrowser 与 MSHTML(2): 获取 window 对象的时机
查看>>
【博客话题】IT人,你肿么了? ——除了IT,你还能选择什么?
查看>>
docker初步入门
查看>>
Outlook提示:无法安装或装载加载项vpmsece.dll
查看>>
使用Apache开源POI和jXLS两种API生成报表
查看>>
oracle控制台OEM无法启动
查看>>
haproxy负载均衡
查看>>
clink 让cmd像ubuntu gnome-terminal一样
查看>>
初识Java模板引擎Beetl之简单示例
查看>>
Oracle UNDO表空间的管理
查看>>
canal.deployer-1.1.0版本,当监听到数据库变动时,server端报异常,docker单核引起的问题...
查看>>