码迷,mamicode.com
首页 > 编程语言 > 详细

java 把一个文件夹里图片复制到另一个文件夹里

时间:2016-09-20 10:19:49      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Calendar;

public class SendServer {

    private int num = 0;
    
    public void process() {
        
        Calendar calendar = Calendar.getInstance();
        String dir = calendar.get(Calendar.YEAR) + "" + getTimeString(calendar.get(Calendar.MONTH) + "");

        String oldPath = "/img2" + dir;
        String newPath = "/img5" + dir;
        
        try {

            while(true){
                System.out.println("复制 " + oldPath + " 目录开始");
                long t1 = System.currentTimeMillis();
                
                num = 0;
                copyFolder(oldPath, newPath);
                
                long t2 = System.currentTimeMillis();
                System.out.println("复制目录结束,用时:" + (t2-t1) + "ms,共复制:" + num + "文件");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void copyFolder(String oldPath, String newPath) {
        
        try {
            File mFile = new File(newPath);
            if(!mFile .exists()){
                (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
            }
            File a = new File(oldPath);
            String[] file = a.list();
            File temp = null;
            for (int i = 0; i < file.length; i++) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file[i]);
                } else {
                    temp = new File(oldPath + File.separator + file[i]);
                }

                if (temp.isFile()) {
                    String fileName = newPath + "/" + (temp.getName()).toString();
                    File testFile = new File(fileName);
                    if (!testFile.exists()) {
                        FileInputStream input = new FileInputStream(temp);
                        FileOutputStream output = new FileOutputStream(fileName);
                        byte[] b = new byte[1024 * 5];
                        int len;
                        while ((len = input.read(b)) != -1) {
                            output.write(b, 0, len);
                        }
                        output.flush();
                        output.close();
                        input.close();
                        num++;
                    }
                }
                if (temp.isDirectory()) {// 如果是子文件夹
                    copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
                }
            }
        } catch (Exception e) {
            System.out.println("复制整个文件夹内容操作出错");
            e.printStackTrace();

        }
    }
    
    private String getTimeString(String time){
        if(time.length()<2){
            return "0" + time;
        }
        else{
            return time;
        }
    }
}

 

java 把一个文件夹里图片复制到另一个文件夹里

标签:

原文地址:http://www.cnblogs.com/margin-gu/p/5887459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!