Number of lines in a file in Java

To get the number of lines in a file in Java, you can use the BufferedReader class and read the file line by line until the end of the file is reached. Here is an example of how to count the number of lines in a file:

int lineCount = 0;

try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {
    while (reader.readLine() != null) {
        lineCount++;
    }
} catch (IOException e) {
    // handle exception
}

System.out.println("Number of lines: " + lineCount);

In the example above, the BufferedReader object reads the file line by line using the readLine() method. The readLine() method returns null when the end of the file is reached.

I hope this helps. Let me know if you have any questions.