Java 多级目录的文件夹拷贝

Java About 859 words

代码

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

}
Views: 1,389 · Posted: 2021-12-08

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

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

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


Today On History
Browsing Refresh