[算法系列] 文件或文件夹的复制(Java)

public class SthHomework02 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入原文件夹地址");
        File src = new File(input.nextLine());
        while (true){
            System.out.println("请输入目的文件夹地址");
            File dest = new File(input.nextLine());
            if (dest.isDirectory()){
                copyFile(src,dest);
                break;
            }else {
                System.out.println("请输入正确的目的文件夹地址");
            }
        }
    }

    private static void copyFile(File src, File dest) {
        try {
            Path path = dest.toPath().resolve(src.toPath().getFileName());
            Files.copy(src.toPath(),path);
            dest = new File(path.toString());
        }catch (IOException e){
            e.printStackTrace();
        }
        if (src.isDirectory()){
            if (src.listFiles()!=null){
                File[] files = src.listFiles();
                for (File file : files){
                    copyFile(file,dest);
                }
            }
        }else {
            return;
        }
    }
}

你可能感兴趣的:(算法,java,算法)