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.