源码和工具下载地址
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(); } } } } }