Breaking News
Home / Java / Java Tutorials / How to count lines in the file using Java?

How to count lines in the file using Java?

Count Lines of a file using JavaIt’s easy to simple count the lines in a file using Unix commands like

grep filename | wc -l

This would return you the number of lines in the file. However if you have to know that programmatically using Java API it is easy too.

All you have to do is to get a handle to the file stream and then in a loop scan through all the lines in the file. For this you can read the lines in the file using the readLine() method, as each line in the file would be separated by a newline character (n) . As you do, increment the lines counter by 1. Finally close the file stream and you would get your answer i.e. the number of lines in the file.

 [pb_builder]

Check Also

Have a Question

How to select a JVM Analyzing Tool?

A JVM monitoring tool also know as a Diagnostic might be needed to fine tune …

Advertisment ad adsense adlogger