问题:?使用递归实现多层文件复制,包含文件夹

问题:只复制所有文件,不包含文件夹
问题相关代码,请勿粘贴截图 !
运行结果及报错内容:暂无
我的解答思路和尝试过的方法 :分别在两个地方使用过mkdirs()方法,但并不管用
我想要达到的结果:实现多层文件复制,包含文件夹
package copy;
import java.io.*;
import java.util.*;
/*复制多层文件夹的所有内容
复制完成后可改名,使用String类的split和lastIndexOf方法切割,后再重新组合
source源文件,target目标*/
public class CopyDir {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("请输入需要复制文件的路径");
            String sourcePath = sc.nextLine();
            System.out.println("请输入复制目标的路径");
            String targetPath = sc.nextLine();
            copyDir(new File(sourcePath), new File(targetPath));
        }
    }
    public static void copyDir(File source, File target) {
        if(source.exists()&target.exists()){
            File newTarget = new File(target, source.getName());
            //newTarget.mkdirs();
            diGui(source, newTarget);
        }else
            System.out.println("输入路径错误!");
    }
    public static void diGui(File file, File newTarget) {
        File[] files = file.listFiles();
        for (File sourceFiles : files) {
            if (sourceFiles.isDirectory()) {
                //newTarget.mkdirs();
                diGui(sourceFiles, newTarget);
            }
            else {
                File newTargetFileName = new File(newTarget, sourceFiles.getName());
                copyFile_1(sourceFiles, newTargetFileName);
            }
        }
    }
    public static void copyFile_1(File source, File target) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(target);
            byte[] bytes = new byte[1024 * 1024];
            int len = 0;
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException("复制失败");
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos != null)
                        fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

感谢,已解决!


package copy;
import java.io.*;
import java.util.*;
/*复制多层文件夹的所有内容
复制完成后可改名,使用String类的split和lastIndexOf方法切割,后再重新组合
source源文件,target目标*/
public class CopyDir {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("请输入需要复制文件的路径");
            String sourcePath = sc.nextLine();
            System.out.println("请输入复制目标的路径");
            String targetPath = sc.nextLine();
            copyDir(new File(sourcePath), new File(targetPath));
        }
    }
    public static void copyDir(File source, File target) {
        if (source.exists() & target.exists()) {
            if (source.isDirectory()) {
                File newSourceFile = new File(target, source.getName());
                if (!newSourceFile.exists())
                    newSourceFile.mkdirs();
                File[] files = source.listFiles();
                for (File file : files) {
                    copyDir(file, newSourceFile);
                }
            } else {
                File file = new File(target, source.getName());
                copyFile_1(source, file);
            }
        } else
            System.out.println("输入路径有误!");
    }
    public static void copyFile_1(File source, File target) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(source));
            bos = new BufferedOutputStream(new FileOutputStream(target));
            byte[] bytes = new byte[1024 * 1024];
            int len = 0;
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException("复制失败");
        } finally {
            try {
                if (bis != null)
                    bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bos != null)
                        bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

    public static void main(String[] args) throws IOException {
        //创建数据源File对象,路径是E:\\itcast
        File srcFile = new File("E:\\itcast");
        //创建目的地File对象,路径是D:\\
        File destFile = new File("MyCharStream\\");
        copyFolder(srcFile,destFile);
    }
        //写方法实现文件夹的复制,参数为数据源File对象和目的地File对象
    private static void copyFolder(File srcFile, File destFile) throws IOException {
        //判断数据源File是否是文件
        if (srcFile.isDirectory()){
            //在目的地下创建和数据源File名称一样的目录
            String srcFileName = srcFile.getName();
            File newFolder = new File(destFile,srcFileName);
            if (!newFolder.exists()){
                newFolder.mkdir();
            }
            //获取数据源File下所有文件或者目录的File数组
            File[] listFiles = srcFile.listFiles();

            //遍历该File数组,得到每一个File对象
            for (File file : listFiles){
                //把该File作为数据源File对象,递归调用复制文件夹的方法
                copyFolder(file,newFolder);
            }
        }else {
            //说明是文件,直接用字节流复制
            File newFile = new File(destFile,srcFile.getName());
            copyFile(srcFile,newFile);
        }
    }

    private static void copyFile(File srcFile, File destFile) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

        int len;
        byte[] bys = new byte[1024];
        while ((len=bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }
        bos.close();
        bis.close();
    }