Java about conditional judgment exercise--count the number of lines of code in all. java files under a src file (comment lines, blank lines not counted)

Source: Internet
Author: User

Requirements: Count the number of lines of code in all. java files under a src file (comment lines, blank lines are not counted)

Analysis: Encapsulates a static method used to statistically determine the number of valid lines of code for a. java file. Use the character buffer stream to read the file, first determine whether it is the beginning of the block comment, and then determine whether it is the end of the block comment, and then determine whether it is a single line of comments or blank lines, if not is a valid code, count the number of rows +1.

For the folder path, the recursive method is used to determine whether the child entry is a file or a folder, and a static statistical method is called by the file. Source:

    

public class Calculaterow {
public static void Main (string[] args) {
/* File File = new file ("D:\\web\\zuoye\\src\\test\\calculaterow.java");
Calculaterow (file); */
File NewFile = new file ("D:\\web\\zuoye\\src");
Calculatefile (NewFile);
file[] files = newfile.listfiles ();
/* for (File f:files) {
System.out.println (f);
}*/
/* int sum = 0;
for (int i = 0; i < files.length; i++) {
if (Files[i].isdirectory ()) {
file[] Filesi = Files[i].listfiles ();
for (int j = 0; J < Filesi.length; J + +) {
int num = Calculaterow (filesi[j]);
Sum+=num;
}
}else{
int num = Calculaterow (files[i]);
sum+= num;
}
}
The number of lines of code for files under the System.out.println (newfile+ "folder total:" +sum); */
}
public static void Calculatefile (File ff) {
int sum = 0;
if (Ff.isdirectory ()) {
file[] files = ff.listfiles ();
for (File f1:files) {
if (F1.isdirectory ()) {
Calculatefile (F1);
}else{
int num =calculaterow (F1);
Sum +=num;
}
}
The number of lines of code for all files under the System.out.println (ff+ "folder total:" +sum);
}else if (Ff.isfile ()) {
int num = Calculaterow (FF);
Sum +=num;
System.out.println (ff+ "file, total number of code lines for all files:" +sum);
}else{
SYSTEM.ERR.PRINTLN ("Input file address is not formatted");
}

}

public static int Calculaterow (File f) {
int num= 0;
if (F.isfile ()) {
try {
BufferedReader br = new BufferedReader (new FileReader (f));
String str;
Boolean flag = false;
while ((Str=br.readline ())!=null) {
String newstr = Str.trim ();
if (Newstr.startswith ("/*")) {
Flag =true;
}else if (Newstr.endswith ("*/")) {
Flag = false;
}
else if (Newstr.startswith ("//") | | Newstr.length () ==0) {
num+=0;
}else if (!flag) {
Num+=1;
}
}
} catch (Exception e) {
E.printstacktrace ();
}
}else {
System.out.println ("Not file!");
}
System.out.println (f+ "file has a code line number of:" +num);
return num;
}
}

Java about conditional judgment exercise--count the number of lines of code in all. java files under a src file (comment lines, blank lines not counted)

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.