I recently wrote a small app that periodically checked the content of a directory. After a while, the app crashed because of too many open file handles. After some debugging
List fileList = null; try (Stream list = Files.list(Paths.get(path.toString()))) { fileList = list.filter(Files::isRegularFile).map(Path::toFile).map(File::getAbsolutePath) .collect(Collectors.toList()); } catch (IOException e) { logger.error("Error occurred while reading email files: ", e); }