IO异常 的处理 test

时间:2021-08-16 17:28:08
 package com.throwsss;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; class Picture{
public static void readWrite(){
File file = new File("D://abc.jpg");
File file2 = new File("F://abc.jpg");
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
inputStream = new FileInputStream(file);
fileOutputStream = new FileOutputStream(file2);
byte[] bs = new byte[1024];
int length = 0;
try {
while((length = inputStream.read(bs))!=-1){
fileOutputStream.write(bs, 0, length);
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}finally{
if(fileOutputStream != null){
try {
fileOutputStream.close();
System.out.println("关闭输出流资源成功...");
} catch (IOException e) {
System.out.println("关闭输出流资源失败...");
throw new RuntimeException(e);
}finally{
if(inputStream != null){
try {
inputStream.close();
System.out.println("关闭输入流对象成功...");
} catch (IOException e) {
System.out.println("关闭输入流对象失敗...");
throw new RuntimeException(e);
}
}
}
}
} }
} public class Throwtest { public static void main(String[] args) {
// TODO Auto-generated method stub Picture picture = new Picture();
picture.readWrite();
} }