天天看點

用輸入輸出位元組流拷貝圖檔,并進行異常處理

package cn.conset.cm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/*拷貝一張圖檔*/
public class Dmeo04 {

    public static void main(String[] args)  {
        // TODO Auto-generated method stub
        FileInputStream input=null;
        FileOutputStream out=null;
        try{
        File file=new File("C:\\Users\\Administrator\\Desktop\\美女.jpg");//定義圖檔位置
        File files=new File("E:\\美女.jpg");//定義拷貝圖檔的地方
         input=new FileInputStream(file);//建立輸入位元組流
         out=new FileOutputStream(files);
        byte[] b=new byte[];//定義緩沖位元組數組
        int length=;
        while((length=input.read(b))!=-){
            out.write(b,,length);

        }
        }catch(Exception e){
            System.out.println("拷貝圖檔出錯");
            throw new RuntimeException(e);
        }finally{
            try{
                if(out!=null){
                out.close();//先進後關
                System.out.println("關閉輸出流成功");
                }
        }catch(Exception e){
            System.out.println("關閉輸出流失敗");
            throw new RuntimeException(e);
        }try{
            if(input!=null){

            input.close();
            System.out.println("關閉輸入流成功");
            }
        }catch(Exception e){
            System.out.println("關閉輸出流失敗");
            throw new RuntimeException(e);
        }
    }
}
}
           

繼續閱讀