Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 小程序 Arthas JVM AQS juc Kubernetes Docker DevOps


Java 多级目录的文件夹拷贝

Java 大约 859 字

代码

public class FilesCopyDemo {

    public static void main(String[] args) throws IOException {
        String source = "D:/abc";
        String target = "D:/abc123";
        Path sourcePath = Paths.get(source);
        Path targetPath = Paths.get(target);

        Files.walk(sourcePath).forEach(path -> {
            try {
                String targetName = path.toString().replace(sourcePath.toString(), targetPath.toString());
                System.out.println("targetName#" + targetName);

                if (Files.isDirectory(path)) {
                    Files.createDirectory(Paths.get(targetName));
                } else if (Files.isRegularFile(path)) {
                    Files.copy(path, Paths.get(targetName));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

}
阅读 697 · 发布于 2021-12-08

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

扫描下方二维码关注公众号和小程序↓↓↓

扫描二维码关注我
昵称:
随便看看 换一批