代码行数统计器

自己写的代码统计器,Java实现的,还有些小缺陷。源码下载地址:http://download.csdn.net/detail/kasogg/4533792


DirScanner.java
01 package com.kaso.codecounter.fileutils;
02 
03 import java.io.*;
04 
05 public class DirScanner {
06      public static void scanning(File baseDir, FileParser fp)
07                throws RuntimeException {
08           if (!baseDir.isDirectory()) {
09                throw new RuntimeException("不是目录");
10           }
11           fp.start(baseDir);
12           try {
13                scanning(baseDir, baseDir, fp);
14           catch (FileParserException e{
15                e.printStackTrace();
16           }
17           fp.end(baseDir);
18      }
19 
20      private static void scanning(File baseDir, File file, FileParser fp)
21                throws FileParserException {
22           if (file.isDirectory()) {
23                fp.startDir(baseDir, file);
24                File[] files file.listFiles();
25                if (files != null{
26                     for (File files{
27                          scanning(baseDir, f, fp);
28                     }
29                }
30                fp.endDir(baseDir, file);
31           else {
32                fp.startFile(baseDir, file);
33           }
34      }
35 }


FileParser.java
01 package com.kaso.codecounter.fileutils;
02 
03 import java.io.*;
04 
05 public interface FileParser {
06      void start(File baseDir);
07 
08      void end(File baseDir);
09 
10      void startDir(File baseDir, File dirthrows FileParserException;
11      
12      void endDir(File baseDir, File dirthrows FileParserException;
13 
14      void startFile(File baseDir, File filethrows FileParserException;
15      
16 }


FileParserException.java
01 package com.kaso.codecounter.fileutils;
02 
03 import java.io.*;
04 
05 public class FileParserException extends IOException {
06      public FileParserException() {
07           super();
08      }
09 
10      public FileParserException(Throwable t{
11           super(t);
12      }
13 
14      public FileParserException(String s{
15           super(s);
16      }
17 
18      public FileParserException(String s, Throwable t{
19           super(s, t);
20      }
21 }


CodeCounterFrame.java
001 package com.kaso.codecounter.view;
002 
003 import java.awt.BorderLayout;
004 import java.awt.FlowLayout;
005 import java.awt.Font;
006 import java.awt.GridLayout;
007 import java.awt.event.ActionEvent;
008 import java.awt.event.ActionListener;
009 import java.io.File;
010 
011 import javax.swing.JButton;
012 import javax.swing.JFileChooser;
013 import javax.swing.JFrame;
014 import javax.swing.JLabel;
015 import javax.swing.JOptionPane;
016 import javax.swing.JPanel;
017 import javax.swing.JScrollPane;
018 import javax.swing.JTextArea;
019 import javax.swing.JTextField;
020 
021 import com.kaso.codecounter.core.CodeCounter;
022 
023 @SuppressWarnings("serial")
024 public class CodeCounterFrame extends JFrame {
025      private JTextArea resultText new JTextArea(2080)// 结果显示窗口
026      private JScrollPane sp new JScrollPane(resultText);// 滚动条
027 
028      private JPanel panel new JPanel();
029      private JPanel dirPanel new JPanel();
030      private JLabel dirLabel new JLabel("选择目录:");
031      private JTextField dirPath new JTextField(50);// 目录路径
032      private JButton chooseBtn new JButton("...");
033 
034      private JPanel typesPanel new JPanel();
035      private JLabel typeLabel new JLabel("文件类型:");
036      private JTextField fileTypes new JTextField(55);// 文件类型
037 
038      private JPanel btnPanel new JPanel();
039      private JButton startBtn new JButton("统计");
040      private JButton exitBtn new JButton("退出");
041 
042      private JFileChooser jfc new JFileChooser(new File("."));
043      private File file;
044 
045      public CodeCounterFrame() {
046           this.init();
047           this.addComponent();
048           this.addListener();
049      }
050 
051      private void init() {
052           this.setDefaultCloseOperation(EXIT_ON_CLOSE);
053           this.setTitle("代码行数统计器");
054           this.setSize(800400);
055           this.setLocation(200100);
056           this.setLayout(new BorderLayout());
057           panel.setLayout(new GridLayout(31));
058           dirPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
059           typesPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
060           jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)// 设置可选中文件类型
061           resultText.setEditable(false);
062           resultText.setFont(new Font("宋体",Font.PLAIN,13));
063      }
064 
065      private void addComponent() {
066           dirPanel.add(dirLabel);
067           dirPanel.add(dirPath);
068           dirPanel.add(chooseBtn);
069 
070           typesPanel.add(typeLabel);
071           typesPanel.add(fileTypes);
072 
073           btnPanel.add(startBtn);
074           btnPanel.add(exitBtn);
075 
076           panel.add(dirPanel);
077           panel.add(typesPanel);
078           panel.add(btnPanel);
079 
080           this.add(sp, BorderLayout.CENTER);
081           this.add(panel, BorderLayout.SOUTH);
082 
083      }
084 
085      private void addListener() {
086           chooseBtn.addActionListener(new ActionListener() {
087                public void actionPerformed(ActionEvent arg0{
088                     int result jfc.showOpenDialog(CodeCounterFrame.this);// 打开文件选择器
089                     if (result == JFileChooser.APPROVE_OPTION// 确定操作
090                          file jfc.getSelectedFile();
091                          if (!file.isDirectory()) {
092                               JOptionPane
093                                         .showMessageDialog(CodeCounterFrame.this,
094                                                   "请选择目录!""警告",
095                                                   JOptionPane.INFORMATION_MESSAGE);
096                               return;
097                          }
098                          dirPath.setText(file.getAbsolutePath())// 路径赋给文本框
099                     }
100                }
101           });
102 
103           startBtn.addActionListener(new ActionListener() {
104                public void actionPerformed(ActionEvent e{
105                     if (dirPath.getText().equals("")) // 判断是否选择文件
106                          JOptionPane.showMessageDialog(CodeCounterFrame.this,
107                                    "请选择目录!""提示"JOptionPane.INFORMATION_MESSAGE);
108                          return;
109                     }
110                     if (fileTypes.getText().trim().equals("")) {
111                          JOptionPane.showMessageDialog(CodeCounterFrame.this,
112                                    "请输入文件类型!""提示"JOptionPane.INFORMATION_MESSAGE);
113                          return;
114                     }
115                     String[] types fileTypes.getText().split("(\\s)+");
116                     if (file == null{
117                          file new File("dirPath");
118                     }
119                     String result CodeCounter.countCode(file, types);
120                     resultText.setText("");
121                     resultText.setText(result);
122                }
123           });
124 
125           exitBtn.addActionListener(new ActionListener() {
126                public void actionPerformed(ActionEvent e{
127                     System.exit(0);
128                }
129           });
130      }
131 
132 }


CodeCounter.java
001 package com.kaso.codecounter.core;
002 
003 import java.io.BufferedReader;
004 import java.io.File;
005 import java.io.FileReader;
006 import java.io.IOException;
007 
008 import com.kaso.codecounter.fileutils.DirScanner;
009 import com.kaso.codecounter.fileutils.FileParser;
010 import com.kaso.codecounter.fileutils.FileParserException;
011 
012 public class CodeCounter {
013      private static StringBuffer result;
014      private static int typesSum; // 某种类型文件的行数
015      private static int dirSum;
016      public static int sum; // 所有文件的总行数
017 
018      public static String countCode(final File file, final String[] types{
019           result new StringBuffer();
020           sum 0;
021           for (int 0types.length; i++{
022                final String type types[i];
023                typesSum 0;
024                dirSum 0;
025                DirScanner.scanning(file, new FileParser() {
026                     public void start(File baseDir{
027                          result.append("开始统计!文件类型为:").append(type).append("\n");
028                     }
029 
030                     public void end(File baseDir{
031                          result.append("--------------------------------------------------------------------------------------------------------\n");
032                          String String.format("%n%-76s %-15s%n%n%n",
033                                    type+"类型文件统计结束,总行数为:"typesSum 行");
034                          result.append(s);
035                     }
036 
037                     public void startDir(File baseDir, File dir)
038                               throws FileParserException {
039                          File[] files dir.listFiles();
040                          for (File files{
041                               if (this.getExtension(f).equalsIgnoreCase(type)) {
042                                    result.append("--------------------------------------------------------------------------------------------------------\n");
043                                    result.append("统计 ").append(dir.getPath())
044                                              .append("\\*").append(type).append("\n\n");
045                                    return;
046                               }
047                          }
048                     }
049 
050                     public void endDir(File baseDir, File dir)
051                               throws FileParserException {
052                          File[] files dir.listFiles();
053                          for (File files{
054                               if (this.getExtension(f).equalsIgnoreCase(type)) {
055                                    String String.format("%n%-85s %-15s%n""小结:",
056                                              dirSum 行");
057                                    result.append(s);
058                                    result.append("--------------------------------------------------------------------------------------------------------\n");
059                                    dirSum 0;
060                                    return;
061                               }
062                          }
063 
064                     }
065 
066                     public void startFile(File baseDir, File file)
067                               throws FileParserException {
068                          int line 0;
069                          if (this.getExtension(file).equalsIgnoreCase(type)) // 如果扩展名相同
070                               line countLine(file);
071                               String String.format("%-80s %-15s%n",
072                                         file.getName()line 行");
073                               result.append(s);
074                          }
075                          dirSum += line;
076                          typesSum += line;
077                          sum += line;
078                     }
079 
080                     private String getExtension(File file{// 获得文件扩展名
081                          String filename file.getAbsolutePath();
082                          int index filename.lastIndexOf('.');
083                          if (index -&& index filename.length()) {
084                               return filename.substring(index);
085                          }
086                          return filename;
087                     }
088 
089                     private int countLine(File file{// 统计文本行数
090                          int line 0;
091                          try {
092                               BufferedReader br new BufferedReader(new FileReader(
093                                         file));
094                               String "";
095                               while ((br.readLine()) != null{
096                                    s.trim();
097                                    if (!s.matches("^[\\s&&[^\\n]]*$")) {
098                                         line++;
099                                    }
100                               }
101                          catch (IOException e{
102                               e.printStackTrace();
103                          }
104                          return line;
105                     }
106 
107                });
108           }
109           result.append("--------------------------------------------------------------------------------------------------------\n");
110           result.append("--------------------------------------------------------------------------------------------------------\n");
111           String String.format("%-85s %-15s%n""总计:"sum 行");
112           result.append(s);
113           return result.toString();
114      }
115 }


CodeCounterMain.java
01 package com.kaso.codecounter.main;
02 
03 import com.kaso.codecounter.view.CodeCounterFrame;
04 
05 public class CodeCounterMain {
06      public static void main(String[] args{
07           CodeCounterFrame ccf new CodeCounterFrame();
08           ccf.setVisible(true);
09           ccf.pack();
10      }
11 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值