Java 8: Copy directory recursively?

后端 未结 6 1654
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 19:47

I see that Java 8 has significantly cleaned up reading the contents of a file into a String:

String contents = new String(Files.readAllBytes(Paths.get(new URI(so         


        
6条回答
  •  深忆病人
    2021-02-03 20:14

    How about the following code

    public void copyFolder(File src, File dest) throws IOException {
            try (Stream stream = Files.walk(src.toPath())) {
                stream.forEachOrdered(sourcePath -> {
    
                    try {
                        Files.copy(
                                /*Source Path*/
                                sourcePath,
                                /*Destination Path */
                                src.toPath().resolve(dest.toPath().relativize(sourcePath)));
                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                });
            }
        }
    

提交回复
热议问题