Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Created May 17, 2017 15:12
Show Gist options
  • Select an option

  • Save douglascayers/a1ffbec636ad3fdf7b101f6d59f7126d to your computer and use it in GitHub Desktop.

Select an option

Save douglascayers/a1ffbec636ad3fdf7b101f6d59f7126d to your computer and use it in GitHub Desktop.
Simple Java method to count all lines in a file. Requires Java 8+
private static int getLineCount( File file ) throws Exception {
try ( LineNumberReader reader = new LineNumberReader( new FileReader( file ) ) ) {
while ( reader.readLine() != null ) {}
return reader.getLineNumber();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment