查找目录文件复制到另一个文件夹下

第一版,此时是没有理解递归思想

import java.io.*;
import java.util.Date;

/**
 * @author chenxinhui
 * @date 2020/5/12
 */
public class SearchFile {
    public static void copyDir(String oldPath, String newPath) throws IOException {
        File file = new File(oldPath);
        //文件名称列表
        String[] filePath = file.list();

        if (!(new File(newPath)).exists()) {
            (new File(newPath)).mkdir();
        }

        for (int i = 0; i < filePath.length; i++) {
            if ((new File(oldPath + file.separator + filePath[i])).isDirectory()) {
                copyDir(oldPath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
            }

            if (new File(oldPath + file.separator + filePath[i]).isFile()) {
                copyFile(oldPath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
            }

        }
    }

    public static void copyFile(String oldPath, String newPath) throws IOException {
        File oldFile = new File(oldPath);
        File file = new File(newPath);
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(file);
        ;

        byte[] buffer = new byte[2097152];

        while ((in.read(buffer)) != -1) {
            out.write(buffer);
        }
    }

    public static void queryFile(String filename, String filePath) throws IOException {

        File file = new File(filePath);
        File[] files = file.listFiles();
        for (File file1 : files) {
            if(file1 !=null && file1.isDirectory()){
                if(filename.equals(file1.getName()) ){
                    copyDir(file1.getAbsolutePath(), "D:/aaa/" +new Date().getTime());
                }else {
                    queryFile(filename,file1.getAbsolutePath());
                }
            }
        }
    }


    public static void main(String[] args) throws IOException {
        queryFile("11w","D:/kidm_info/IMG_DOC");


//        File file = new File("D:\\20200402");
//        if (file != null && file.isDirectory()) {
//            File[] files = file.listFiles();
//            for (File file11 : files) {
//                if (file11 != null && file11.isDirectory()) {
//                    File[] files1 = file11.listFiles();
//                    for (File file22 : files1) {
//                        File[] files2 = file22.listFiles();
//                        for (File file33 : files2) {
//                            System.out.println(file33.getName());
//                            if (file33.getName().equals("0s3")) {
//                                copyDir(file33.getAbsolutePath(), "D:/aaa");
//                            }
//                        }
//                    }
//                }
//            }
//        }
    }
}

第二版,这版可以查找指定文件,注意,文件为文件夹,把此文件夹下的文件复制到指定位置

package com.richness.File;

import java.io.*;

/**
 * @author chenxinhui
 * @date 2020/5/22
 */
public class findFileUtils {
    public static void main(String[] args) throws IOException {
        String[] data = {"0ea"};
        for (int i = 0; i < data.length; i++) {
            FindFile(new File("D:\\kidm_info\\IMG_DOC"), data[i]);
        }
    }


    public static void copyFile(String oldFile, String newFile) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(oldFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
        int len;
        byte[] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        bos.close();
        bis.close();
    }

    public static void FindFile(File dir, String filename) throws IOException {
        if (dir.exists() && dir.isDirectory()) {
            File[] files = dir.listFiles();
            if (files != null && files.length > 0) {
                for (File file : files) {
                    if (file.getName().equals(filename)) {
                        File[] targeFiles = file.listFiles();
                        for (File targeFile : targeFiles) {
                            File targetDir = new File("D:\\cxh_zhuanyong\\SWS\\" + file.getParentFile().getName());
                            if (!targetDir.exists()) {
                                targetDir.mkdirs();
                            }
                            copyFile(targeFile.getAbsolutePath(), targetDir.getAbsolutePath() + "\\" + targeFile.getName());
                        }
                    } else {
                        FindFile(file, filename);
                    }
                }
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。