Count Number of Lines in a File in Java

Learn to count all the lines in a file in Java using Stream of lines and LineNumberReader class. In all listed solutions, we are iterating over the lines until the last line is encountered.

1. Counting Lines using Stream of Lines

The Files.lines() method can be used to get the stream of lines from a specified text file. Then we can use stream.count() method for counting the elements in the stream.

Note that the file is closed by closing the stream. So to keep things less verbose, we can use try-with-resources statement that automatically closes the stream after use.

This approach does not require the whole file to be present in the memory so it is an efficient solution for large files.

String fileName = "c:/temp";
long noOfLines = -1;

try (Stream<String> fileStream = Files.lines(Paths.get(fileName))) {
	//Lines count
  	noOfLines = (int) fileStream.count();
}

2. Using LineNumberReader

The LineNumberReader is an input stream reader that keeps track of line numbers. By default, line numbering begins at 0.

We can count the total lines in the file by LineNumberReader reference for the file and skip() to the last line of the file. At this moment, we can get the line number count with getLineNumber().

This solution is also efficient for large text files.

String fileName = "c:/temp";
long noOfLines = -1;

try(LineNumberReader lineNumberReader =
    new LineNumberReader(new FileReader(new File(fileName)))) {
    //Skip to last line
  	lineNumberReader.skip(Long.MAX_VALUE);
  	noOfLines = lineNumberReader.getLineNumber() + 1;
}

3. Using Files.readAllLines() for Small files

We can use the initial two solutions for small files also. But if the file is small and we are going to refer to file content later the application code then it makes sense to read the whole file into memory.

The readAllLines() method reads the whole file and returns all the lines as the List of strings. To count all lines, use list.size() method.

This solution requires the whole file to be present in memory so it is not an efficient solution for large files.

String fileName = "c:/temp";
long noOfLines = -1;

try(LineNumberReader lineNumberReader =
    	new LineNumberReader(new FileReader(new File(fileName)))) {
  	lineNumberReader.skip(Long.MAX_VALUE);
  	noOfLines = lineNumberReader.getLineNumber() + 1;
}

4. Conclusion

In this short Java tutorial, we learned to find the total number of lines in a file using Java. We saw the efficient solutions line Stream and LineNumberReader, and also we saw poor solutions like reading the whole file into List.

It is advised to try each solution for performance comparison to find the best solution to your requirements.

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode