java文件拷贝---将一个相册拷贝到另一个相册

来源:互联网 发布:mac 快捷键设置没有用 编辑:程序博客网 时间:2024/06/17 19:08

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("自定义流关闭异常");            }        }    }}
0 0
原创粉丝点击