【踩坑记事】大量文件处理时,java.nio.file.Files.list导致“打开的文件太多”

错误的用法

Files.list(directory).forEach(path -> {
    // do
});

 

正确的用法

try (Stream<Path> stream = Files.list(directory)) {
    stream.forEach(path -> {
        // do
    });
} catch (Exception e) {
    //e.printStackTrace();
}

使用 try-with-resources 释放资源

posted on 2020-09-25 10:15  小小程序员的梦想  阅读(923)  评论(0编辑  收藏  举报

导航