how to check the lines in a file java scanner

Java
int count = 0;
while (scanner.hasNextLine()) {
    count++;
    scanner.nextLine();
}
Source

Also in Java: