Hi : Given an arbitrary file (java), I want to count the lines.
This is easy enough, for example, using Apache's FileUtils.readLines(...) method...
However, for large files, reading a whole file in place is ludicrous (i.e. just to count lines).
One home-grown option : Create BufferedReader or use the FileUtils.lineIterator function, and count the lines.
However, I'm assuming there could be a (low memory), up to date API for doing simple large File operations with a minimal amount of boiler plate for java --- Does any such library or functionality exist anywhere in the any of the Google, Apache, etc... open-source Java utility libraries ?
With Guava:
which won't hold the whole file in memory or anything.
Without a library:
Java 8 short way:
But most memory effiecint:
You do not need String objects in this task at all.
Here's a version that makes use of Apache Commons IO library. You can pass
null
forencoding
to pick the platform default.