Sometimes, when archiving a project, we will inevitably encounter statistics on the number of lines of code. In this case, we can use a piece of code to directly calculate the number of lines.
Below is an example code for counting the number of lines of code in a Java project:
Import Java. io. file; import jodd. io. fileutil; public class test {public static void main (string [] ARGs) throws exception {file root = new file ("E:/myeclipse 9/"); showallfiles (Root ); system. out. println (n);} static int N; Final Static void showallfiles (File DIR) throws exception {file [] FS = dir. listfiles (); For (INT I = 0; I <FS. length; I ++) {system. out. println (FS [I]. getabsolutepath (); If (FS [I]. getabsolutepath (). endswith (". java ") {string [] STR = fileutil. readlines (FS [I]. getabsolutepath (); For (string S: Str) {If (S. length ()> 0) {n ++; // system. out. println (s) ;}} if (FS [I]. isdirectory () {try {showallfiles (FS [I]);} catch (exception e ){}}}}}
Calculate the number of lines of code under all packages in a project