Ad Code

Ticker

6/recent/ticker-posts

Java - How to get the total number of lines count from multiple files in a directory

In this Post we are going to write code to get the total number of lines count from each files in a directory. Here I am going to use java.nio, java.io and java.util.stream packages.

In this example I am counting the number of lines from files with fileExtension as ".java" from the below Directory, But one can use the same program to count the number of lines of multiple ".json",".yaml", ".xml", ".text", ".vm" any other extensions of files under a directory.


So Here is the Code snippet through which get us the total number of lines from each file under a Directory.


Here First We will find the file Path as shown code snippet:  


Then Get the File Name:

Then count the number of lines using BufferedReader:

Now Its time to Execute the Program

This is the output we have:

File MomoController.java  has  113 number of lines
File MomoWithdrawalController.java  has  34 number of lines
File SMSController.java  has  63 number of lines

Process finished with exit code 0

Summary:

So in this simple java code example we have learned how o get the total number of lines count from  multiple files in a directory. 

GitHub Link : rajivksingh13/teachlea
Please feel free to provide your valuable comments, Thanks.

Ref:

https://mkyong.com/java/how-to-get-the-total-number-of-lines-of-a-file-in-java/

Post a Comment

0 Comments

Ad Code