Java 8: Copy directory recursively?

后端 未结 6 1653
伪装坚强ぢ
伪装坚强ぢ 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:09

    In this way the code looks a bit simpler

    import static java.nio.file.StandardCopyOption.*;
    
    public  void copyFolder(Path src, Path dest) throws IOException {
        try (Stream stream = Files.walk(src)) {
            stream.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
        }
    }
    
    private void copy(Path source, Path dest) {
        try {
            Files.copy(source, dest, REPLACE_EXISTING);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    

提交回复
热议问题