-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCodeCounter
139 lines (125 loc) · 3.92 KB
/
CodeCounter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JTextArea;
public class CodeCounter {
static long normalLines = 0;//代码正常行数
static long commentLines = 0;//注释行数
static long whiteLines = 0;//空行数
static String str = "";
public static void main(String[] args) {
File f = new File("D:/Program Files/MyEclipse 6.0/WorkSpace1.5/ToolClass/src");//统计该目录下所有的以“.java”结束的文件行数
File[] codeFiles = f.listFiles();
/*for(File child : codeFiles){
if(child.getName().matches(".*\\.java$")) {
parse(child);
}
}*/
System.out.println(f.getName());
tree(f, 1);
System.out.println("normalLines:" + normalLines);
System.out.println("commentLines:" + commentLines);
System.out.println("whiteLines:" + whiteLines);
System.out.println("allLines:" + (normalLines+commentLines+whiteLines));
}
public static void count(String file,JTextArea area) {
File f = new File(file);//统计该目录下所有的以“.java”结束的文件行数
File[] codeFiles = f.listFiles();
/*for(File child : codeFiles){
if(child.getName().matches(".*\\.java$")) {
parse(child);
}
}*/
System.out.println(f.getName());
area.append(f.getName());
area.append(tree(f, 1,area));
System.out.println("normalLines:" + normalLines);
System.out.println("commentLines:" + commentLines);
System.out.println("whiteLines:" + whiteLines);
System.out.println("allLines:" + (normalLines+commentLines+whiteLines));
String str = "normalLines:" + normalLines+"\n"+"commentLines:"
+ commentLines+"\n"+"whiteLines:" + whiteLines+"\n"+"allLines:" + (normalLines+commentLines+whiteLines);
area.append(str);
}
//统计 f 目录下(包括子目录)所有的以“.java”结尾文件的行数
private static void tree(File f, int level){
String preStr = "";
for(int i = 0; i < level; i++){
preStr += " ";
}
File []childs = f.listFiles();
for(int i = 0; i<childs.length; i++){
if(childs[i].getName().matches(".*\\.java$")) {
parse(childs[i]);
}
System.out.println(preStr + childs[i].getName());
if(childs[i].isDirectory()){
tree(childs[i], level + 1);
}
}
}
//统计 f 目录下(包括子目录)所有的以“.java”结尾文件的行数
private static String tree(File f, int level, JTextArea area){
String preStr = "";
for(int i = 0; i < level; i++){
preStr += " ";
}
File []childs = f.listFiles();
for(int i = 0; i<childs.length; i++){
if(childs[i].getName().matches(".*\\.java$")) {
parse(childs[i]);
}
System.out.println(preStr + childs[i].getName());
//area.append("\n"+preStr + childs[i].getName());
str = str+preStr + childs[i].getName()+"\n";
if(childs[i].isDirectory()){
tree(childs[i], level + 1,area);
}
}
return str;
}
//统计 f 目录下(不包括子目录) 所有的以".java"结尾的文件行数
private static void parse(File f) {
BufferedReader br = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader(f));
String line = "";
while((line = br.readLine()) != null) {
line = line.trim();
if(line.matches("^[\\s&&[^\\n]]*$")) {
whiteLines ++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
commentLines ++;
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(br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}