Number of lines of code under Scan folder

Source: Internet
Author: User

A whim to know how many lines of code are in the project. Took a little time to write a preliminary demo, the principle is very simple, scan the project src directory, get all the files read by line, each read a row, cumulative plus 1.

The difficulty: How to skip the directory and get the file under the directory and the next level of the directory, where recursion is better.

Deficiencies: The recognition of comments is not supported, the identification of code submitter is not supported, time classification is not supported, file type classification, the total character statistics are not supported.

Improve the way: single-line comments easier, trim the judge whether to start with//, or with a regular match, more than a bit of trouble, may want to read the entire file content, and then through regular matching/***/and/**/content, other functions are not very difficult, interested students can realize their own.

Code

public class Scanmain {public static void main (string[] args) {int codeLine = 0;        Code Scan path String scanpath = "F:\\PROTOCOLCONVERT\\SRC"; File RootFile = new file (Scanpath);
In fact, there is no need to initialize, the method is all written as static on the line, here is set before the global variable scanmain SM = new Scanmain (); list<file> files = new arraylist<> (); Files = Sm.scan (Getallfiles (RootFile), files); System.out.println (Files.size ()); for (File fn:files) {Codeline+=sm.countcode (FN); } System.out.println (CodeLine); }//reads the file by line and accumulates the number of rows public int countcode (file file) {int i = 0; try {InputStreamReader read = new InputStreamReader (new FileInputStream (file), "UTF-8");//test Consider the encoding format BufferedReader BufferedReader = new BufferedReader (read); String line= ""; while (line = Bufferedreader.readline ())!=null) {i++; }} catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } return i; }//This step seems to be superfluous. public static file[] Getallfiles (File f) {File[] files = f.listfiles (); return files; }//using recursive scanning of folders, all directories continue to look down and all files are stored in result. Public list<file> Scan (file[] files,list<file> result) {for (File fn:files) {if (fn.isdirect Ory ()) {Scan (fn.listfiles (), result); }else{System.out.println ("Shi"); Result.add (FN); }} return result; }}

  

Number of lines of code under Scan folder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.