-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc_2
187 lines (185 loc) · 7.17 KB
/
wc_2
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package Test;
import java.io.BufferedReader;//文件读取的包
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class MyTest {
private static String command = "";
private static String[] sParameter;
private static String sFilename;
private static int linecount = 0;//行数
private static int wordcount = 0;//单词数
private static int charcount = 0;//字符数
private static int nullLinecount = 0; // 空行数
private static int codeLinecount = 0; // 代码行数
private static int noteLinecount = 0; // 注释行数
private static JFileChooser chooser;
private static int flag_x = 0;
public static void main(String[] args) {
command = ""; // 存储用户命令
while (true) {
int flag_s = 0;
System.out.print("please input path! ");
Scanner s = new Scanner(System.in);//表示从键盘输入
command = s.nextLine();//得到输入命令
// 图形化界面
if (command.equals("-x")) {
ResponseforX();
continue;
}
String[] arrMessSplit = command.split(" "); // 分割命令,以“ ”作为标志分割,
int iMessLength = arrMessSplit.length;
sParameter = new String[iMessLength - 1];
// 获取命令参数数组
for (int i = 0; i < iMessLength - 1; i++) {
sParameter[i] = arrMessSplit[i];
if (sParameter[i].equals("-s")) {
flag_s = 1;
}
}
// 获取文件名
sFilename = arrMessSplit[iMessLength - 1];
if (flag_s == 1) {
ResponseforS();//"-s"命令
} else {
BaseAndAdvanceCount();//高级功能调用
Response();//基础的一些功能调用
}
}
}
//图形化界面
private static void ResponseforX() {
// TODO Auto-generated method stub
flag_x = 1;
chooser = new JFileChooser();
int value = chooser.showOpenDialog(null);
if (value == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
sFilename = file.getAbsolutePath();
BaseAndAdvanceCount();
Response();
}
}
//"-s"递归返回符合条件的文件
private static void ResponseforS() {
// TODO Auto-generated method stub
String path = sFilename.substring(0, sFilename.indexOf("*") - 1);
String type = sFilename.substring(sFilename.indexOf("*") + 1, sFilename.length());
System.out.print("Path:");
System.out.println(path + "\n");
File dir = new File(path);
if (dir.isDirectory()) {
File next[] = dir.listFiles();
for (int i = 0; i < next.length; i++) {
if (!next[i].isDirectory()) {
// System.out.println(next[i].getName());
if (next[i].getName().substring(next[i].getName().indexOf("."), next[i].getName().length())
.equals(type)) {
System.out.println(next[i].getName());
sFilename = path + "\\" + next[i].getName();
BaseAndAdvanceCount();
Response();
}
}
}
} else {
System.out.println("path error");
}
}
//输出函数
private static void Response() {
if (flag_x == 1) {
System.out.print("line count:");
System.out.println(linecount);
System.out.print("word count:");
System.out.println(wordcount);
System.out.print("char count:");
System.out.println(charcount);
System.out.print("blank lines count:");
System.out.println(nullLinecount);
System.out.print("code lines count:");
System.out.println(codeLinecount);
System.out.print("note lines count:");
System.out.println(noteLinecount);
} else {
for (int i = 0; i < sParameter.length; i++) {
if (sParameter[i].equals("-l")) {
System.out.print("line count:");
System.out.println(linecount);
} else if (sParameter[i].equals("-w")) {
System.out.print("word count:");
System.out.println(wordcount);
} else if (sParameter[i].equals("-c")) {
System.out.print("char count:");
System.out.println(charcount);
} else if (sParameter[i].equals("-a")) {
System.out.print("blank lines count:");
System.out.println(nullLinecount);
System.out.print("code lines count:");
System.out.println(codeLinecount);
System.out.print("note lines count:");
System.out.println(noteLinecount);
}
}
}
System.out.println("");
}
//高级功能
private static void BaseAndAdvanceCount() {
linecount = 0;
wordcount = 0;
charcount = 0;
nullLinecount = 0;
codeLinecount = 0;
noteLinecount = 0;
File file = new File(sFilename);
if (file.exists()) {
try {
//文件流读取文件
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = "";
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
linecount++;
sb.append(line);
charcount += line.length();
//去掉首尾空格
line = line.trim();
// 空白行
if (line == "" || line.length() <= 1) {
nullLinecount++;
continue;
}
// 注释行
int a = line.indexOf("/");
int b = line.substring(a + 1).indexOf("/");
if (b == 0) {
noteLinecount++;
continue;
}
// 代码行
codeLinecount++;
}
wordcount = sb.toString().split("\\s+").length;//
br.close();
isr.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("path error");
}
}
}