代码统计工具java 实现

简介: 2.核心处理统计方法。
源码和工具下载地址
http://download.csdn.net/detail/wangshuai6707/9497405
http://download.csdn.net/detail/wangshuai6707/9497404
</pre><pre code_snippet_id="1655032" snippet_file_name="blog_20160420_1_7495149" name="code" class="java">用windowbuilder 写的图形界面
package com.ws.view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import com.ws.dmtjtool.CodeCounter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
public class Main extends JFrame {
  private JPanel contentPane;
  private JTextField textField;
  private JTextField textField_1;
  private JTextField textField_2;
  private JTextField textField_3;
  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          Main frame = new Main();
          frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }
  /**
   * Create the frame.
   */
  public Main() {
    setIconImage(Toolkit.getDefaultToolkit().getImage("1.ico"));
    setTitle("帅子代码统计工具V1.0");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 518, 340);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    JPanel panel = new JPanel();
    panel.setBounds(5, 5, 424, 1);
    contentPane.add(panel);
    panel.setLayout(null);
    JLabel label = new JLabel("输入你的工程路径:");
    label.setBounds(5, 30, 116, 15);
    contentPane.add(label);
    textField = new JTextField();
    textField.setBounds(120, 27, 161, 21);
    contentPane.add(textField);
    textField.setColumns(10);
    JLabel label_1 = new JLabel("如:F:/myProject/ECP-OPEN/");
    label_1.setBounds(291, 30, 186, 15);
    contentPane.add(label_1);
    JButton button = new JButton("点击开始统计");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        textField_1.setText("");
        textField_2.setText("");
        textField_3.setText("");
        CodeCounter.codeLines=0;
        CodeCounter.commentLines=0;
        CodeCounter.blankLines=0;
        CodeCounter.fileArray=new ArrayList<File>();
        String path="";
        path=textField.getText();
        path = path.replace("/", "\\");
        File file = new File(path);//F:\myProject\ECP-OPEN
        if(!path.matches("^[A-z]:\\\\(.+?\\\\)*$")){
          JOptionPane.showMessageDialog(null, "工程路径不合法!", "温馨提示", JOptionPane.ERROR_MESSAGE); 
        }else{
          if (!file.exists()) {
            JOptionPane.showMessageDialog(null, "工程路径不存在!请核对", "温馨提示", JOptionPane.ERROR_MESSAGE); 
          }else{
            ArrayList<File> al = CodeCounter.getFile(file);
            for (File f : al) {
              if (f.getName().matches(".*\\.java$")) // 匹配java格式的文件
                CodeCounter.count(f);
            }
            textField_1.setText(CodeCounter.codeLines+"");
            textField_2.setText(CodeCounter.commentLines+"");
            textField_3.setText(CodeCounter.blankLines+"");
          }
        }
      }
    });
    button.setBounds(321, 72, 131, 55);
    contentPane.add(button);
    JLabel lblNewLabel = new JLabel("统计结果展示:");
    lblNewLabel.setBounds(15, 64, 131, 15);
    contentPane.add(lblNewLabel);
    JLabel lblNewLabel_1 = new JLabel("代码行数:");
    lblNewLabel_1.setBounds(5, 92, 73, 15);
    contentPane.add(lblNewLabel_1);
    JLabel lblNewLabel_2 = new JLabel("注释行数:");
    lblNewLabel_2.setBounds(5, 128, 91, 15);
    contentPane.add(lblNewLabel_2);
    JLabel lblNewLabel_3 = new JLabel("空白行数:");
    lblNewLabel_3.setBounds(5, 159, 91, 15);
    contentPane.add(lblNewLabel_3);
    textField_1 = new JTextField();
    textField_1.setEnabled(false);
    textField_1.setBounds(120, 89, 138, 21);
    contentPane.add(textField_1);
    textField_1.setColumns(10);
    textField_2 = new JTextField();
    textField_2.setEnabled(false);
    textField_2.setBounds(120, 125, 138, 21);
    contentPane.add(textField_2);
    textField_2.setColumns(10);
    textField_3 = new JTextField();
    textField_3.setEnabled(false);
    textField_3.setBounds(120, 156, 138, 21);
    contentPane.add(textField_3);
    textField_3.setColumns(10);
    JLabel lblNewLabel_4 = new JLabel("更多常用工具请访问我们冰点科技团队,草根吧论坛网(www.caogen8.co)");
    lblNewLabel_4.setBounds(15, 184, 487, 21);
    contentPane.add(lblNewLabel_4);
    JLabel lblNewLabel_5 = new JLabel("更多精美UI设计素材,请访问ui设计之家(www.uiuehome.com)");
    lblNewLabel_5.setBounds(15, 215, 487, 15);
    contentPane.add(lblNewLabel_5);
    JLabel lblNewLabel_6 = new JLabel("我们的官网:www.bdkjzx.com,联系作者QQ2374212111");
    lblNewLabel_6.setBounds(10, 251, 472, 15);
    contentPane.add(lblNewLabel_6);
    JButton btnNewButton = new JButton("清空统计结果");
    btnNewButton.setIcon(new ImageIcon("41c643d15d_32.ico"));
    btnNewButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        textField_1.setText("");
        textField_2.setText("");
        textField_3.setText("");
        CodeCounter.codeLines=0;
        CodeCounter.commentLines=0;
        CodeCounter.blankLines=0;
      }
    });
    btnNewButton.setBounds(321, 137, 131, 23);
    contentPane.add(btnNewButton);
  }
}

2.核心处理统计方法。

package com.ws.dmtjtool;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class CodeCounter {
  public static long codeLines = 0;
  public static long commentLines = 0;
  public static long blankLines = 0;
  public static ArrayList<File> fileArray = new ArrayList<File>();
  /**
   * 可以统计指定目录下以及其子目录下的所有java文件中代码
   * 
   * @author wangshuai
   */
  public static void main(String[] args) {
    String path="F:/myProject/ECP-OPEN/";
    path = path.replace("/", "\\");
    File file = new File(path);//F:\myProject\ECP-OPEN
    System.out.println(path.matches("^[A-z]:\\\\(.+?\\\\)*$"));
    if(!path.matches("^[A-z]:\\\\(.+?\\\\)*$")){
      System.out.println("请输入正确路径");
    }
    if (!file.exists()) {
        System.out.println("文件夹不存在");
    }
    ArrayList<File> al = getFile(file);
    for (File f : al) {
      if (f.getName().matches(".*\\.java$")) // 匹配java格式的文件
        count(f);
    }
    System.out.println("代码行数:" + codeLines);
    System.out.println("注释行数:" + commentLines);
    System.out.println("空白行数: " + blankLines);
  }
  // 获得目录下的文件和子目录下的文件
  public static ArrayList<File> getFile(File f) {
    File[] ff = f.listFiles();
    for (File child : ff) {
      if (child.isDirectory()) {
        getFile(child);
      } else
        fileArray.add(child);
    }
    return fileArray;
  }
  // 统计方法
  public static void count(File f) {
    BufferedReader br = null;
    boolean flag = false;
    try {
      br = new BufferedReader(new FileReader(f));
      String line = "";
      while ((line = br.readLine()) != null) {
        line = line.trim(); // 除去注释前的空格
        if (line.matches("^[ ]*$")) { // 匹配空行
          blankLines++;
        } else if (line.startsWith("//")) {
          commentLines++;
        } else if (line.startsWith("/*") && !line.endsWith("*/")) {
          commentLines++;
          flag = true;
        } else if (line.startsWith("/*") && line.endsWith("*/")) {
          commentLines++;
        } else if (flag == true) {
          commentLines++;
          if (line.endsWith("*/")) {
            flag = false;
          }
        } else {
          codeLines++;
        }
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
          br = null;
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}


目录
相关文章
|
16天前
|
Java 测试技术 应用服务中间件
常见 Java 代码缺陷及规避方式(下)
常见 Java 代码缺陷及规避方式(下)
43 0
|
17天前
|
Java
Java中ReentrantLock释放锁代码解析
Java中ReentrantLock释放锁代码解析
25 8
|
20天前
|
前端开发 小程序 Java
uniapp上传图片 前端以及java后端代码实现
uniapp上传图片 前端以及java后端代码实现
34 0
|
22天前
|
设计模式 存储 Java
23种设计模式,享元模式的概念优缺点以及JAVA代码举例
【4月更文挑战第6天】享元模式(Flyweight Pattern)是一种结构型设计模式,旨在通过共享技术有效地支持大量细粒度对象的重用。这个模式在处理大量对象时非常有用,特别是当这些对象中的许多实例实际上可以共享相同的状态时,从而可以减少内存占用,提高程序效率
35 4
|
22天前
|
设计模式 Java 中间件
23种设计模式,适配器模式的概念优缺点以及JAVA代码举例
【4月更文挑战第6天】适配器模式(Adapter Pattern)是一种结构型设计模式,它的主要目标是让原本由于接口不匹配而不能一起工作的类可以一起工作。适配器模式主要有两种形式:类适配器和对象适配器。类适配器模式通过继承来实现适配,而对象适配器模式则通过组合来实现
32 4
|
23天前
|
存储 缓存 算法
优化 Java 后台代码的关键要点
【4月更文挑战第5天】本文探讨了优化 Java 后台代码的关键点,包括选用合适的数据结构与算法、减少不必要的对象创建、利用 Java 8 新特性、并发与多线程处理、数据库和缓存优化、代码分析与性能调优、避免阻塞调用、JVM 调优以及精简第三方库。通过这些方法,开发者可以提高系统性能、降低资源消耗,提升用户体验并减少运营成本。
|
24天前
|
Java 开发工具 流计算
flink最新master代码编译出现Java Runtime Environment 问题
在尝试编译Flink源码时遇到Java运行时环境致命错误:EXCEPTION_ACCESS_VIOLATION。问题出现在JVM.dll+0x88212。使用的是Java 11.0.28和Java HotSpot(TM) 64-Bit Server VM。系统为Windows客户端,没有生成核心dump文件。错误日志保存在hs_err_pid39364.log和replay_pid39364.log。要解决这个问题,建议检查JDK版本兼容性,更新JDK或参照错误报告文件提交Bug至http://bugreport.java.com/bugreport/crash.jsp。
|
26天前
|
Java
使用Java代码打印log日志
使用Java代码打印log日志
82 1
|
26天前
|
设计模式 Java 数据库
Java设计模式精讲:让代码更优雅、更可维护
【4月更文挑战第2天】**设计模式是解决软件设计问题的成熟方案,分为创建型、结构型和行为型。Java中的单例模式确保类仅有一个实例,工厂方法模式让子类决定实例化哪个类。适配器模式则协调不兼容接口间的合作。观察者模式实现了一对多依赖,状态变化时自动通知相关对象。学习和适当应用设计模式能提升代码质量和可维护性,但需避免过度使用。设计模式的掌握源于实践与不断学习。**
Java设计模式精讲:让代码更优雅、更可维护
|
26天前
|
SQL 设计模式 安全
Java单例模式几种写法以及代码案例拿来直接使用
Java单例模式几种写法以及代码案例拿来直接使用
32 0