Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better.files.File#lines does not close the file #51

Closed
holgerbrandl opened this issue Nov 24, 2015 · 2 comments
Closed

better.files.File#lines does not close the file #51

holgerbrandl opened this issue Nov 24, 2015 · 2 comments
Labels

Comments

@holgerbrandl
Copy link
Contributor

Example:

val someFile = File("test.txt")
someFile.write("tt")

for(a<-1 to 10000) {
    someFile.lines
}

will die inevitably with a "Too many open files in system" error.

The solution would be to use some closable or even simpler to use Files.readAllLines instead of Files.lines

See also
http://www.rationaljava.com/2015/02/java-8-pitfall-beware-of-fileslines.html
https://bugs.openjdk.java.net/browse/JDK-8073923

@pathikrit pathikrit added the bug label Nov 25, 2015
@pathikrit
Copy link
Owner

I think auto closable is the right way here. If file has a lot of lines (say a log file), it may cause an OOM with File.readAllLines

@holgerbrandl
Copy link
Contributor Author

It might be hard to implement it that way since b.f.File.lines is returning an iterator (which is nice since it allows for very elegant programming). I've not much knowledge about auto-closables but I thought when using them, the consumption of the file content would need to happen in a scope, which would reduce the beauty/readability of File.lines.

I totally agree about potential performance issues when using java.nio.Files.allLines as backend for b.f.File.lines but at least programs would not die after a while when using it. The provided links indicate that nio.Files.lines API design is misleading to many users and maybe b.f could do a better job by providing

  • a file line iterator (e.g. .linesIterator) by which enforces file closing via some auto-closable.
  • a more simplistic .lines method which simply returns a List[String]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants