IO异常的处理

IO异常的处理

public class Demo1 {
	public static void main(String[] args) {
		try {
			readTest();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void readTest() throws IOException {
		try {
			File file = new File("G:\\aaaa.txt");
			FileInputStream fileInputStream = new FileInputStream(file);
			byte[] buf = new byte[1024];
			int length = 0;
			while((length = fileInputStream.read(buf)) != -1) {
				System.out.println(new String(buf, 0, length));
			}
		} catch(IOException e) {
			throw e; // 直接抛出IOException时,方法上也得声明抛出,并且强迫调用者要处理异常,这样很不灵活...
		}
		// 关闭资源	这样是关不掉的,因为一旦上面出了问题,下面的语句就执行不到了,所以要放在finally块里才能关闭。
		fileInputStream.close();
	}
}
public class Demo2 {
	public static void main(String[] args) {
		readTest();
	}
	public static void readTest() {
		FileInputStream fileInputStream = null;
		try {
			// 找到目标文件
			File file = new File("G:\\a.txt");
			// 建立数据的输入通道
			fileInputStream = new FileInputStream(file);
			// 创建缓冲数组输入数据
			byte[] buf = new byte[1024];
			int length = 0;
			while((length = fileInputStream.read(buf)) != -1) {
				System.out.println(new String(buf, 0, length));
			}
		} catch(IOException e) {
			// 处理的代码	首先要阻止后面代码执行,而且需要通知调用者这里出错了...
			System.out.println("读取文件资源出错了...");
			throw new RuntimeException(e); // 把IOException传递给RuntimeException包装一层然后再抛出,这样做的目的是为了让调用者代码(可处理可不处理)更加灵活。
		} finally {
			try {
				if(fileInputStream != null) { // 创建的文件对象的路径可能不存在。当不存在时输入数据管道建立不了,fileInputStream调用close方法就会出现空指针异常,所以在这里要判断一次。
					fileInputStream.close();
					System.out.println("关闭资源成功!");
				}
			} catch(IOException e) {
				System.out.println("关闭资源出错了...");
				throw new RuntimeException(e);
			}
		}
	}
}
/**
* 需求:处理拷贝图片例子的异常
*/
public class Demo3 {
	public static void main(String[] args) {
		copy();
	}
	public static void copy() {
		FileOutputStream fileOutputStream = null;
		FileInputStream fileInputStream = null;
		try {
			// 找到目标文件
			File inFile = new File("G:\\1.jpg");
			File destFile = new File("F:\\1.jpg");
			// 建立数据的输入输出通道
			fileInputStream = new FileInputStream(inFile);
			// 每创建一个FileOutputStream时,默认情况下,FileOutputStream的指针会指向文件的开始位置。而当FileOutputStream创建后,每写出一次数据,指针都会移动到文件的末尾处。因此这里是没有必要加true的。
			fileOutputStream = new FileOutputStream(destFile);
			// 建立缓冲数组,边读边写
			byte[] buf = new byte[1024];
			int length = 0;
			while((length = fileInputStream.read(buf)) != -1) {
				fileOutputStream.write(buf, 0, length); // 如果不指定长度,拷贝过去的图片就很可能比源图片大!
			}
		} catch(IOException e) {
			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 {
				try {
					if(fileInputStream != null) {
						fileInputStream.close();
						System.out.println("输入流对象关闭成功!");
					}
				} catch(IOException e) {
					System.out.println("输入流对象关闭失败..");
					throw new RuntimeException(e);
				}
			}
		}
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值