Java 统计代码的小工具

来源:未知 责任编辑:智问网络 发表时间:2013-11-04 19:49 点击:

统计代码的小工具,程序功能不是很完善,欢迎大家多给意见

  1. import java.awt.event.ActionEvent;  
  2. import java.awt.event.ActionListener;  
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileReader;  
  7. import java.io.IOException;  
  8. import javax.swing.JFileChooser;  
  9. import javax.swing.JFrame;  
  10. import javax.swing.JMenu;  
  11. import javax.swing.JMenuBar;  
  12. import javax.swing.JMenuItem;  
  13. import javax.swing.JScrollPane;  
  14. import javax.swing.JTextArea;  
  15. import javax.swing.UIManager;  
  16.  
  17. public class CodeCounterFrame extends JFrame  
  18. {  
  19.     JMenuBar menuBar = new JMenuBar();  
  20.     JMenu fileMenu = new JMenu("文件");  
  21.     JMenuItem openMenuItem = new JMenuItem("打开");  
  22.     JTextArea txa = new JTextArea();  
  23.     JScrollPane jsp =new JScrollPane(txa);  
  24.     String output = "";  
  25.     static long codeLines = 0;  
  26.     static long commentLines = 0;  
  27.     static long blankLines = 0;  
  28.     public CodeCounterFrame()  
  29.     {  
  30.         this.setJMenuBar(menuBar);  
  31.         menuBar.add(fileMenu);  
  32.         fileMenu.add(openMenuItem);  
  33.         txa.setEditable(false); //txa里面的内容不能被编辑  
  34.         add(jsp);  
  35.           
  36.         //内部匿名类监听器  
  37.         openMenuItem.addActionListener(new ActionListener()  
  38.         {  
  39.             public void actionPerformed(ActionEvent e)  
  40.             {  
  41.                 if(e.getSource() == openMenuItem)  
  42.                 {  
  43.                     //点击了打开菜单  
  44.                     //弹出文件选择器  
  45.                     JFileChooser chooser = new JFileChooser();  
  46.                     chooser.showOpenDialog(null);  
  47.                       
  48.                     //获取选中的文件  
  49.                     File selectedFile = chooser.getSelectedFile();  
  50.                     BufferedReader br = null;  
  51.                     boolean flag = false;  
  52.                     try {  
  53.                          FileReader reader = new FileReader(selectedFile);//读取文档的内容  
  54.                          br = new BufferedReader(reader);  
  55.                          String line = "";  
  56.                            
  57.                         //2读文件  
  58.                         while ((line = br.readLine()) != null)  
  59.                         {  
  60.                             line = line.trim(); // 除去注释前的空格  
  61.                             if (line.matches("^[ ]*$"))   
  62.                             {   
  63.                                 // 匹配空行  
  64.                                 blankLines++;  
  65.                             }   
  66.                             else if (line.startsWith("//"))   
  67.                             {  
  68.                                 commentLines++;  
  69.                             }   
  70.                               else if (line.startsWith("/*") && !line.endsWith("*/"))  
  71.                              {  
  72.                                 commentLines++;  
  73.                                 flag = true;  
  74.                             }   
  75.                             else if (line.startsWith("/*") && line.endsWith("*/"))   
  76.                             {  
  77.                                 commentLines++;  
  78.                             }   
  79.                             else if (flag == true)   
  80.                             {  
  81.                                 commentLines++;  
  82.                                 if (line.endsWith("*/"))   
  83.                                 {  
  84.                                     flag = false;  
  85.                                 }  
  86.                             }   
  87.                             else   
  88.                             {  
  89.                                 codeLines++;  
  90.                             }  
  91.                         }  
  92.  
  93.                         //将结果保存到output  
  94.                         output+=("代码行数:" + codeLines+"\n"+"注释行数:" + commentLines+"\n" 
  95.                                   +"空白行数: " + blankLines+"\n"+"总行数:" + (codeLines+commentLines+blankLines));  
  96.                           
  97.                         //将统计的结果在txa里面显示  
  98.                         txa.setText(output);  
  99.                     }   
  100.                     catch (FileNotFoundException ex)   
  101.                     {  
  102.                         ex.printStackTrace();  
  103.                     }   
  104.                     catch (IOException ex)   
  105.                     {  
  106.                         ex.printStackTrace();  
  107.                     }   
  108.                     finally   
  109.                     {  
  110.                         if (br != null)   
  111.                         {  
  112.                             try {  
  113.                                 br.close();  
  114.                                 br = null;  
  115.                             }   
  116.                             catch (IOException ex)   
  117.                             {  
  118.                                 ex.printStackTrace();  
  119.                             }  
  120.                         }  
  121.                     }  
  122.                 }  
  123.             }  
  124.         });  
  125.     }  
  126.       
  127.     //Frame主程序  
  128.        public static void main(String[] args)   
  129.        {  
  130.            try {  
  131.                  UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //windows界面风格  
  132.             } catch (Exception e) {  
  133.                 e.printStackTrace();  
  134.             }  
  135.             CodeCounterFrame frame = new CodeCounterFrame();  
  136.             frame.setTitle("统计代码的小工具");  
  137.             frame.setSize(300,200);  
  138.             frame.setVisible(true);  
  139.             frame.setLocationRelativeTo(null);//窗体居中显示  
  140.             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  141.        }  
  142. }  
  143.       
  144.       
  145.       
  146.  
  147.       
  148.  

运行的窗口:

 输出的结果:

 

 

本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/624394

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Java编程语言的八大优点
    • JVM对象生命周期详细介绍
    • Java平台上的CRM系统
    • Java 算数测试小程序
    • Command(命令模式)
    • Java 一个简单的画图程序
    • Java环境 使用Resin在NT环境下配置JSP环境
    • Java 日历的小程序
    • Java 统计代码的小工具
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1