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

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;
        }
    }
}

 -----------------------2017/3/22---------------------------------------

获得文件夹下所有文件名

    public static class TreeNode {
        /**节点名称**/
        private String text; 
        /**节点路径**/
        private String fileName; 
         /**子节点**/
        private List<TreeNode> children = new ArrayList<TreeNode>();
    }
    
    public static TreeNode readfile(TreeNode tn){
        try {

            File file = new File(tn.fileName);
            tn.text = file.getName();
            if (!file.isDirectory()) {
                //System.out.println(file.getName());
                
            } else if (file.isDirectory()) {
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(tn.fileName + "\" + filelist[i]);
                    TreeNode tn1 = new TreeNode();
                    tn1.text = readfile.getName();
                    tn1.fileName = tn.fileName + "\" + filelist[i];
                    if (!readfile.isDirectory()) {
                        tn.children.add(tn1);
                    } else if (readfile.isDirectory()) {
                        tn.children.add(readfile(tn1));
                    }
                }
            }

        } catch (Exception e) {
            System.out.println("readfile()   Exception:" + e.getMessage());
        }
        return tn;
    }
    
    public static void main(String[] args){
        TreeNode tn = new TreeNode();
        tn.fileName = "C:\Users\42955\Desktop\文件夹\软件";
        readfile(tn);
        System.out.println(tn);
        System.out.println(tn.text);
    }
原文地址:https://www.cnblogs.com/margin-gu/p/5887459.html