Hi : Given an arbitrary file (java), I want to count the lines.
This is easy enough, for example, using Apache\'s FileUtils.readLines(...) method...
Howeve
Java 8 short way:
Files.lines(Paths.get(fileName)).count();
But most memory effiecint:
try(InputStream in = new BufferedInputStream(new FileInputStream(name))){
byte[] buf = new byte[4096 * 16];
int c;
int lineCount = 0;
while ((c = in.read(buf)) > 0) {
for (int i = 0; i < c; i++) {
if (buf[i] == '\n') lineCount++;
}
}
}
You do not need String objects in this task at all.