zoukankan      html  css  js  c++  java
  • 代码量统计

      最近无聊,想知道自己3年来敲了多少代码。于是自己就动手写了,留下个足迹吧

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.WindowConstants;
    
    public class coderCounter extends JFrame{
        private long normalLines = 0;
        private long commentLines = 0;
        private long whiteLines = 0;
        private JButton jButton1;
        private JTextArea jTextArea1;
        private JScrollPane jScrollPane1;
        private JTextField jTextField1;
        private static final String LINE_SEPARATOR = System.getProperty("line.separator");
        private static final long serialVersionUID = 1L;
        private FilterBySuffix fileter = new FilterBySuffix(".java");
        
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    coderCounter inst = new coderCounter();
                    inst.setLocationRelativeTo(null);
                    inst.setVisible(true);
                }
            });
            
        }
        
        public  coderCounter() {
            initGUI();
        }
        
        private void initGUI() {
            try {
                setTitle("代码量统计小软件");
                setResizable(false);
                setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                getContentPane().setLayout(null);
                {
                    jButton1 = new JButton();
                    getContentPane().add(jButton1);
                    jButton1.setText("u786eu5b9a");
                    jButton1.setBounds(466, 21, 128, 50);
                    jButton1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jButton1ActionPerformed(evt);
                        }
                    });
                }
                {
                    jTextField1 = new JTextField();
                    getContentPane().add(jTextField1);
                    jTextField1.setBounds(17, 21, 437, 50);
                    jTextField1.setText("输入Eclispe的工作空间路径,可以统计代码量,注释,空白行数");
                    jTextField1.addKeyListener(new KeyAdapter() {
                        public void keyPressed(KeyEvent evt) {
                            jTextField1KeyPressed(evt);
                        }
                    });
                }
                {
                    jScrollPane1 = new JScrollPane();
                    getContentPane().add(jScrollPane1);
                    jScrollPane1.setBounds(17, 83, 577, 272);
                    {
                        jTextArea1 = new JTextArea();
                        jTextArea1.setEditable(false);
                        jScrollPane1.setViewportView(jTextArea1);
                    }
                }
                pack();
                this.setSize(626, 401);
            } catch (Exception e) {
                //add your error handling code here
                e.printStackTrace();
            }
        }
        
        private void jButton1ActionPerformed(ActionEvent evt) {        
            showDir();
        }
    
        private void showDir() {
            //通过在文本框输入具体的目录,将目录中的当前文件或者文件夹的名称列出到文本区域中。
            /*
             * 思路:
             * 1,获取文本框中的路径。
             * 2,将路径封装成File对象。;
             */
            
            //1,获取路径。通过文本框对象完成。
        String str_dir = jTextField1.getText();    
            //2,将字符串路径封装成File对象。
            File dir = new File(str_dir);
            //遍历
            getFilelist(dir);      
            jTextArea1.append("代码量:"+normalLines+LINE_SEPARATOR);
            jTextArea1.append("注释量::" + commentLines+LINE_SEPARATOR);
            jTextArea1.append("空白行:" + whiteLines+LINE_SEPARATOR);
        }
        
        private void jTextField1KeyPressed(KeyEvent evt) {
            
            if(evt.getKeyCode()==KeyEvent.VK_ENTER){
                showDir();
            }
            
        }
        
        private  void getFilelist(File dir){
            /**
             * 对指定目录进行递归。
             * 多级目录下都要用到相同的集合和过滤器。那么不要在递归方法中定义,而是不断的进行传递。
             * @param dir 需要遍历的目录。
             * @param filter 接收指定的过滤器。
             */
            File [] files = dir.listFiles();//通过listFiles方法,获取dir当前下的所有的文件和文件夹对象。
            
            for(File f:files){  //遍历文件夹数组,如果是文件夹,继续遍历。
                if(f.isDirectory() && !f.isHidden() && f.canWrite()){
                    getFilelist(f);
                }else 
                    if(fileter.accept(f)){//通过过滤器对文件进行筛选。。               
                           parse(f);
                           jTextArea1.append(f+LINE_SEPARATOR);                
                }  
            }
        }
        private void parse(File file) {
            
            BufferedReader bf = null;
            boolean comment = false;
            
            try {
                bf = new BufferedReader(new FileReader(file));
                String line = "";
                while((line = bf.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 (comment) {
                        commentLines ++;
                        if(line.endsWith("*/")) {
                            comment = false;
                        }
                    } else if (line.startsWith("//")) {
                        commentLines ++;
                    } else {
                        normalLines ++;
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(bf != null)
                    try {
                        bf.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }    
        }
    }
    class FilterBySuffix implements FilenameFilter {
        private String suffix;
        public FilterBySuffix(String suffix) {
            this.suffix = suffix;
        }
        public boolean accept(File dir) {
    
            return dir.getName().endsWith(suffix);
        }
    
    }
    interface FilenameFilter {
        public boolean accept(File dir);
    }
    天行健,君子以自强不息
  • 相关阅读:
    python3图片转化成字符画
    ubuntu 18.04安装PIL(Python Imaging Library )
    Ubuntu 18.04安装钉钉
    django 使用iframe跨域请求
    django 自定义日志字段
    Ubuntu18.04下安装搜狗输入法(亲测有效)
    Nginx 配置指令手册
    js闭包Demo
    自己写了一个无缝滚动的插件(jQuery)
    写JQuery 插件 什么?你还不会写JQuery 插件
  • 原文地址:https://www.cnblogs.com/god-y-l/p/6736412.html
Copyright © 2011-2022 走看看