java文件拷貝---將一個相冊拷貝到另一個相冊

java文件拷貝
將一個相冊拷貝到另一個相冊
循環將一個目錄下的所有圖片拷貝到另一個目錄下面

package com.wxhl.fsy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IODouble {

    /**
     * 循環拷貝圖片
     * 1、先循環獲取指定目錄下所有圖片的文件名
     * 2、再循環拷貝圖片
     * @param args
     */
    public static void main(String[] args) {

        String origPath = "E:\\其它\\照片\\2015-北京Z君";
        //拷貝到桌面用時:23775毫秒
//      String copyPath = "C:\\Users\\Administrator\\Desktop\\Picture";
        //拷貝到同一個文件夾下用時:6852毫秒
        String copyPath ="E:\\其它\\照片\\newPic";
        String[] filePath = getFilePath(origPath);
        long time = System.currentTimeMillis();
        for (String file : filePath) {

            /**
             * 調用的時候處理異常,用戶就可以看到該異常
             */

            try {

                fileCopy(origPath + "\\+1" + file,copyPath + "\\" + file);
            } catch (MyFileNotFoundException e) {

                e.printStackTrace();
            } catch (MyIOException e) {

                e.printStackTrace();
            }
        }
        System.out.println(System.currentTimeMillis() - time);
    }

    /**
     * 獲取指定路徑下的指定文件名
     * @param filePaths
     * @return
     */
    public static String[] getFilePath(String filePaths){

        String[] filePath;
        filePath = new File(filePaths).list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {

                return (name.endsWith(".jpg") || name.endsWith(".png")) && !(new File(dir + name).isDirectory());
            }
        });
        return filePath;
    }

    /**
     * 單個文件拷貝
     * @param origFile 原文件所在位置
     * @param copyFile 拷貝文件所在位置
     * @throws MyIOException 自定義IO異常
     * @throws MyFileNotFoundException 自定義文件找不到異常
     */
    public static void fileCopy(String origFile,String copyFile)
            throws MyIOException, MyFileNotFoundException{

        InputStream is = null;
        OutputStream os = null;
        try {

            is = new FileInputStream(new File(origFile));//若創建流失敗,則拋出異常,在try{}catch{}塊中可以捕獲
            os = new FileOutputStream(new File(copyFile));
            byte[] bytes = new byte[1024];
            int length = 0;

            while((length = is.read(bytes))!=-1){   

                os.write(bytes, 0, length);
            }

        } catch (FileNotFoundException e) {//既可以在try{}catch{}中捕獲異常,又能將異常拋給方法調用者

            e.printStackTrace();
//          throw new MyFileNotFoundException("自定義文件找不到異常");//拋出自定義異常
        } catch (IOException e) {

            throw new MyFileNotFoundException("自定義IO異常");
        }finally{

            try {

                if(is != null){

                    is.close();
                }
                if(os != null){

                    os.flush();
                    os.close();
                }
            } catch (IOException e) {

                throw new MyIOException("自定義流關閉異常");
            }
        }

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章