日期:2014-05-20 浏览次数:20889 次
package countingprogram;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
public class CodeCounting {
public static void main(String[] args) {
new MyCountingFrame("程序代码统计小程序");
}
}
class MyCountingFrame extends Frame {
private static final long serialVersionUID = 1L;
static int sum = 0,realsum = 0,space = 0,note = 0;
static String b = " ";
TextArea ta1 ;
TextField t1;
Label l1 ;
MyCountingFrame(String s){
super(s);
setBounds(300,300,400,400);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
});
Panel p1 = new Panel();
l1 = new Label("请输入你要查询的路径");
t1 = new TextField(20);
Button b1 = new Button("确定");
b1.addActionListener(new MyListener());
p1.add(t1);
p1.add(l1);
p1.add(b1);
ta1 = new TextArea(20,10);
add(p1,BorderLayout.SOUTH );
add(ta1,BorderLayout.CENTER );
setVisible(true);
}
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e){
ta1.setText(" ");
String pathname = t1.getText();
if(b.equals(pathname)){
ta1.append("输入重复");
}else if(pathname.matches("^.+:\\\\+.*")){
l1.setText("计算中");
new Counting (pathname);
l1.setText("计算完成");
b = t1.getText();
t1.setText(" ");
}else if(pathname.matches("^.+:\\\\.+\\\\$")){
l1.setText("计算中");
new Counting (pathname);
l1.setText("计算完成");
b = t1.getText();
t1.setText(" ");
}else{
ta1.setText("输入错误");
ta1.append("/ 或者\\ 的符号请用 ' \\\\ '来代替");
}
}
}
class Counting {
String pathname;
ArrayList<File> fileall = new ArrayList<File>();
Counting(String pathname){//构造函数,运行主程序
this.pathname = pathname;
launch();
sum = 0;
realsum = 0;
space = 0;
note = 0;
}
public void launch(){//检查后缀名并运行搜索程序。
File file = new File(pathname);
/*File [] file1 = file.listFiles();
for(File c:file1){
fileall.add(c);
}*/
myFileList(file);//递归调用
for(File child:fileall){
if(child.getName().matches(".*\\.java$")){
check(child);
}
ta1.setText("sum:" + sum + " \n" + "note:" + note + "\n" + "space:" + space + "\n" + "realsum:" + realsum);
}
}
public void myFileList(File f){
File[] file2 = f.listFiles();
for(File kid : file2){
if(kid.isDirectory()){
myFileList(kid);
}
fileall.add(kid);
}
}
private void check( File child2 ) {//search program is starting...
BufferedReader br = null;
String line;
boolean comment = false;
try{
br = new BufferedReader(new FileReader(child2));
while((line = br.readLine()) != null){
sum++;
line = line.trim();
if(line.matches("[\\s&&[^\\n]]*$")){
space++;
}else if(line.startsWith("/*")&& !line.endsWith("*/")){
note++;
comment = true;
}else if(true == comment){
note++;
if(line.endsWith("*/")){
comment = false;
}
}else{
realsum++;
}
}
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}