BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
// Get lines in a file
public int linesInFile(String path) {
try {
return (int) Files.lines(Paths.get(path)).count(); // Get lines and convert to integer
} catch(IOException e) {
e.printStackTrace(); // Print error if file does not exist.
}
return -1; // Return -1, if file does not exist.
}