Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement Code Statistics Mini Program by java

Shulou Source: shulou.com Published: 2022-06-03 09:46:11 10月06日 Update

This article mainly introduces java how to achieve code statistics Mini Program, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The details are as follows

You can test your weekly workload.

Package rexExp; import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException; public class CodeCounter {/ / three static variables store rows static long normalLines = 0; static long commentLines = 0; static long whileLines = 0; public static void main (String [] args) {String pathname = "E:\ testeclipseworkspace\\ JavaLearn\\ src\ collection"; File file = new File (pathname); File [] codeFiles = file.listFiles () / / find all subfiles under the folder / / files must end with .java, use regular expressions to validate for (File child: codeFiles) {if (child.getName (). Matches (". *\\ .java $") {parse (child);}} System.out.println ("normalLines:" + normalLines); System.out.println ("commentLines:" + commentLines); System.out.println ("whileLines:" + whileLines) } private static void parse (File file) {BufferedReader bReader = null; boolean comment = false; try {bReader = new BufferedReader (new FileReader (file)); / / read each line String line= ""; while ((line=bReader.readLine ())! = null) {line= line.trim (); / / remove leading and trailing spaces / / count the number of blank lines if (line.matches ("^ [\ slots & [^\ n]] * $")) {whileLines++ } / / count the number of lines of comments else if (line.startsWith ("/ *") & &! line.endsWith ("* /")) {commentLines++; / / if "/ *" is encountered, the comment starts with comment = true;} else if (line.startsWith ("/ *") & & line.endsWith ("* /")) {commentLines++;} else if (true = = comment) {commentLines++ If (line.endsWith ("* /")) {comment = false;}} else if (line.startsWith ("/ /")) {commentLines++;} else {normalLines++;} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {if (bReader! = null) {try {bReader.close ();} catch (IOException e) {e.printStackTrace () }}} Thank you for reading this article carefully. I hope the article "how to implement Code Statistics Mini Program in java" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Tags: Statistics articles files code programs comments one line three values interests content variables at the same time workload folders more friends rules knowledge spaces Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information OPPO Reno Shulou Tech Info MySQL