KITFORMA · EDITORIAL ANSWER
Why does repeatedly reading Files.lines eventually exhaust open file handles?
KitForma editorial guide. A lazily read stream may still own an open file. Completing a terminal operation does not replace the resource owner’s close obligation.
Step-by-step answer
try (var lines = Files.lines(path, StandardCharsets.UTF_8)) { return lines.filter(this::accept).count(); }. Do not return that stream from a method whose try block already closed it. If returning a lazy stream is necessary, document that the caller must close it.
Run repeated reads of a small fixture and confirm another process can replace or remove the file after each completed operation where the platform permits it. This addresses resource lifetime; it does not bound memory if the pipeline later collects an enormous file into a list.Sources and verification
Sources checked:
Scope: This editorial guide is based on the cited sources and tool behavior. A forum question or a query observed for our site does not establish market search volume, low competition, guaranteed rankings or inadequate answers elsewhere.
This editorial answer was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.