您的位置:首页 > 其它

IO异常 的处理 test

2015-08-04 23:31 176 查看
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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: