IO异常 的处理
2015-09-06 08:47
190 查看
package cn.itcast.exception; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import javax.management.RuntimeErrorException; /* IO异常 的处理 */ public class Demo1 { public static void main(String[] args) { // readTest(); copyImage(); } // 拷贝图片 public static void copyImage() { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { // 找到目标文件 File inFile = new File("F:\\美女\\1.jpg"); File outFile = new File("E:\\1.jpg"); // 建立输入输出通道 fileInputStream = new FileInputStream(inFile); fileOutputStream = new FileOutputStream(outFile); // 建立缓冲数组,边读边写 byte[] buf = new byte[1024]; int length = 0; while ((length = fileInputStream.read(buf)) != -1) { fileOutputStream.write(buf, 0, length); } } catch (IOException e) { System.out.println("拷贝图片出错..."); throw new RuntimeException(e); } finally { // 关闭资源 try { if (fileOutputStream != null) { fileOutputStream.close(); System.out.println("关闭输出流对象成功..."); } } catch (IOException e) { System.out.println("关闭输出流资源失败..."); throw new RuntimeException(e); } finally { if (fileInputStream != null) { try { fileInputStream.close(); System.out.println("关闭输入流对象成功..."); } catch (IOException e) { System.out.println("关闭输入流对象失败..."); throw new RuntimeException(e); } } } } } public static void readTest() { FileInputStream fileInputStream = null; try { // 找到目标文件 File file = new File("F:\\aaaaa.txt"); // 建立数据输入通道 fileInputStream = new FileInputStream(file); // 建立缓冲数组读取数据 byte[] buf = new byte[1024]; int length = 0; while ((length = fileInputStream.read(buf)) != -1) { System.out.print(new String(buf, 0, length)); } } catch (IOException e) { /* * //处理的代码... 首先你要阻止后面的代码执行,而且要需要通知调用者这里出错了... throw new * RuntimeException(e); * //把IOException传递给RuntimeException包装一层,然后再抛出,这样子做的目的是 * 为了让调用者使用变得更加灵活。 */ System.out.println("读取文件资源出错...."); throw new RuntimeException(e); } finally { try { if (fileInputStream != null) { fileInputStream.close(); System.out.println("关闭资源成功..."); } } catch (IOException e) { System.out.println("关闭资源失败..."); throw new RuntimeException(e); } } } }
相关文章推荐
- java.io.IOException: Cannot run program "C:\Program": CreateProcess error=2,
- 8月31日,测试数据库IO异常,忙碌了一天
- 服务器IO异常-Ubuntu14.04【日常工作记录】
- The Network Adapter could not establish the connection解决方案
- IO异常 Too many open files linux处理
- mysql表去除重复的记录
- 输出字节流
- 【HTML】html 小技巧
- 2015-08-25
- linux任务计划
- 输出字节流
- Android Studio中获取sha1证书指纹数据的方法
- 强刷固件|解锁与重锁|刷写内核REC|恢复DRM噪点等等|综合
- IO流分类---输入流
- squid缓存服务的搭建和配置管理
- sublime text保存时删除行尾空格
- UIViewAnimationOptions类型
- Java反射机制剖析(一)-定义和API
- html静态网页制作中div+css的浅运用
- javascript中的select、checkbox