编程知识 cdmana.com

How to use java to simply implement a code line count counter

hello , I'm Xiaohei , I learned recently java After the input and output flow of the heart has been itching , Always looking for something to do , So use java Code to achieve the following statistics code all lines of code , Take a look at how many lines I've played since I went to college .

Attach the implementation code first !

package InOutStream;
import java.util.* ;
import java.io.* ;

class codeCount {
   
   
	private static int count ; // Total number of Statistics 
	private static int countCPP ;//CPP
	private static int countJAVA ;//java
	private static int countPY ;//python
	private String path ; // Path to the folder used to receive user input to save code 
	private int reading(String path) throws Exception {
   
    // This function is used to count the number of lines in a code file 
		FileReader reader = new FileReader(path) ;
		BufferedReader buffer = new BufferedReader(reader) ;
		int count = 0 ;
		while(buffer.readLine()!=null) {
   
   
			count ++ ;
		}
		buffer.close() ;
		reader.close() ;
		return count ;
	}
	private void caculate(String nowpath) throws Exception{
   
   // Counting function 
		File nowfile = new File(nowpath) ;
		if (nowfile.isFile()) {
   
   
			if (nowpath.endsWith(".cpp")) {
   
   
				int sum = reading(nowpath) ;
				countCPP += sum ;
				count += sum ;
			}
			else if (nowpath.endsWith(".py")) {
   
   
				int sum = reading(nowpath) ;
				countPY += sum ;
				count += sum ;
			}
			else if (nowpath.endsWith(".java")) {
   
   
				int sum = reading(nowpath) ;
				countJAVA += sum ;
				count += sum ;
			}
			else {
   
   
				System.out.println(nowpath.substring(nowpath.indexOf("."))+": This type of file does not belong to the code file or the statistical function of the code file is under development , Coming soon !");
			}
		}
		else {
   
    // If this path represents a folder , The recursive operation is performed 
			String []filesset = nowfile.list() ;
			for (String i:filesset ) {
   
   
				String newpath = nowpath + nowfile.separator + i ;// Synthesis path 
				caculate(newpath) ;
			}
		}
	}
	public codeCount(String src) {
   
   
		path = src ;
	}
	public static int getLinesCPP() {
   
   
		return countCPP ;
	}
	public static int getLinesJAVA() {
   
   
		return countJAVA ;
	}
	public static int getLinesPY() {
   
   
		return countPY ;
	}
	public static int getLines() {
   
   
		return count ;
	}
	public void caculator() throws Exception {
   
    // External packaging 
		this.caculate(path) ;
	}
	public String toString() {
   
    // rewrite toString Method 
		return  " The statistics are as follows :\n" +
				"cpp Row number :\n"+countCPP +
				"\njava Row number :\n"+countJAVA +
				"\npython Row number :\n"+countPY ;
	}
}

public class Count{
   
   
	public static void main(String []args) throws Exception {
   
   
		Scanner cin = new Scanner(System.in) ;
		System.out.println(" Please enter the address :");
		String path = cin.next() ;
		codeCount machine = new codeCount(path) ;
		machine.caculator(); 
		System.out.println(machine.toString());
		cin.close();
	}
}

example :
I saved a folder on my desktop to save the code :
 Insert picture description here
This is what it looks like when you open it :
 Insert picture description here
Take the path :
 Insert picture description here
Run the program , Paste the path into the program






 Insert picture description here
give the result as follows !!!
 Insert picture description here
This is all code pulling !! If you have any other implementation methods or comments or suggestions , Welcome to bring it up in the comments section !


ps: Because I only learned c、cpp、java、python. So the code only for these kinds of Statistics , You are welcome to modify the code to meet your needs !!

Next I'll learn swing, At that time, we will try to generate the code into a graphical interface . Welcome to pay close attention to me !!!

Thank you for reading !!

版权声明
本文为[osc_lg0msmnd]所创,转载请带上原文链接,感谢

Scroll to Top