[java] Number of lines in a file in Java

I tested the above methods for counting lines and here are my observations for Different methods as tested on my system

File Size : 1.6 Gb Methods:

  1. Using Scanner : 35s approx
  2. Using BufferedReader : 5s approx
  3. Using Java 8 : 5s approx
  4. Using LineNumberReader : 5s approx

Moreover Java8 Approach seems quite handy :

Files.lines(Paths.get(filePath), Charset.defaultCharset()).count()
[Return type : long]