统计java代码小工具

最近做一个项目,eclipse版本没有带统计代码行数的插件,公司还不能随便上网。所以在网上找了找资料,发现用java做的多少都稍有问题,便自己改了改,实现如下:统计代码行数:包括java,jsp,js文件。统计可以按照单个文件统计,也可以按照目录统计(包含子目录)。
以下是运行后的截图和部分代码。

[img]http://s17.photo.store.qq.com/http_imgload.cgi?/rurl4_b=aa39a986443207181c218edd411e412328d0e6e855b613afe810560015a1b2f90876b7b69e02ceb0795356effa617ab32e3d2501c6b3fb642233f3d21b5df7df0ae64b8888a928cc0d3cfbba3281628955f72d2d/tj[/img]
package codeComprehensive;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class CountCodes extends JFrame{
private static final long serialVersionUID = 1L;
private long codeLines = 0; // 单个文件的代码行数
private long commentLines = 0; // 单个文件的注释行数
private long allBlankLines = 0; // 单个文件的空行行数
private long allLines = 0; // 单个文件的总行数
private long allCodeLines = 0; // 总代码行数
private long allCcommentLines = 0; // 总的注释行数
private long blankLines = 0; // 总空行行数
private long allFilesLines =0;// 多个文件的总行数
private File fileName;

private JLabel pathLabel;
private JTextField pathField;
private JButton openButton;
private JButton clearButton;
private JPanel panelNorth, panelSouth, clearPanel;
private ArrayList<File> list = new ArrayList<File> ();
private String[] tableHeader = {"File Name", "Code Lines", "Comment Lines", "Blank Lines", "All Lines"};
private Vector<String> vectorHeader = new Vector<String> ();
// 统计结果结合
private Vector<Vector> values = new Vector<Vector> ();
// 综合统计行数据
private Vector<String> allValues = new Vector<String> ();
private JTable table;
private DefaultTableModel tableModel;
private JScrollPane scrollPane;
public CountCodes(){
super("Welcome to use Count Codes --wujingyong");
pathLabel = new JLabel("File Directory : ");
pathField = new JTextField(100);
pathField.setEditable(false);
openButton = new JButton("Open");
clearButton = new JButton("Clear");
openButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
每次统计前,先清空之前的统计结果/
int a = tableModel.getRowCount();
while(a>0)
{
a--;
tableModel.removeRow(a);
}


openFile();

if (fileName == null) {
return;
}
// 如果清空了统计记录,则重新统计;否则包含之前的统计记录
if (tableModel.getRowCount() == 0) {
list = new ArrayList<File>();
}
analysisFileOrDir(fileName);

for(File file : list){
analyze(file);
}
追加最后一行总数的统计//
if (values.size()!=0 ) {
// 清空之前的最后统计数据
allValues = new Vector<String>();
// 追加本次统计的最后综合数据
allValues.add("Lines SUM :");
allValues.add(String.valueOf(allCodeLines));
allValues.add(String.valueOf(allCcommentLines));
allValues.add(String.valueOf(allBlankLines));
allFilesLines = allCodeLines + allCcommentLines + allBlankLines;
allValues.add(String.valueOf(allFilesLines));
values.add(allValues);
// 统计后,清空各项综合统计数据
zeroAllLines();

}
//
}
});
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
int a = tableModel.getRowCount();
while(a>0)
{
a--;
tableModel.removeRow(a);
}
}
});
panelNorth = new JPanel();
clearPanel = new JPanel();
clearPanel.add(clearButton);
clearPanel.add(openButton);
panelNorth.setLayout(new BorderLayout());
panelNorth.add(pathLabel, BorderLayout.WEST);
panelNorth.add(pathField, BorderLayout.CENTER);
panelNorth.add(clearPanel, BorderLayout.EAST);
for (int i=0; i<tableHeader.length; i++) {
vectorHeader.add(tableHeader);
}
// 追加统计结果
tableModel = new DefaultTableModel(values, vectorHeader);

table = new JTable(tableModel);

scrollPane = new JScrollPane(table);

panelSouth = new JPanel();
panelSouth.setLayout(new BorderLayout());
panelSouth.add(scrollPane, BorderLayout.CENTER);

setLayout(new BorderLayout());
add(panelNorth, BorderLayout.NORTH);
add(panelSouth, BorderLayout.CENTER);

setBounds(222,200,512,400);
setVisible(true);
}

public static void main(String args[]){
CountCodes countCodes = new CountCodes();
countCodes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

代码统计插件 eclipse plugin java 提供xls导出。 liugang594 开发的,方便下载 这是一款小型的代码统计软件。它具有以下功能: 1、良好的统计性能。可以统计各种方式的注释,如单行、多行注释;注释在行前 、行尾、单独成行等。 2、可以进行多种语言注释统计。默认情况下已经可以统计.java、.C、.h、.CPP 、.C++和.AS 类型文件的统计了。还可以统计.properties文件 3、可以进行统计类型的自定义。点击统计类型配置按钮,在弹出的对话框左边是剩余可 配置的文件类型,右边是当前设置的统计类型,可以通过加入或移除按钮进行增删。 4、方便的打开文件。你可以在树或表上双击文件各直接打开文件或目录。双击文件时会 三种打开方式:本程序、系统、eclipse。如果是eclipse工程里的文件,则默认 是使用eclipse打开,否则使用本程序打开 5、可以进行多个目录的统计。每个统计的目录都会作为树结点加到树上去。当选择统计 的目录已经统计,则更新该目录。选择一个统计的目录,点del键时可删除这个统计 结点 6、单击统计结果表格的表头时,会按正反序进行排序,文件名按字母顺序排序;其他统计 数字按数字大小进行排序! 关于扩展的使用方法: 扩展就是通过继承basecount包里的BaseCount类,实现它的countTypeFile方法来达到 扩展的目的。你只要把传入的参数里的文件内容读取出来,然后按照你自定义的判断 注释、内容、总数、包声明、导入数、空白行数然后一个一个的设入到传入的result参 数里就可以了(没有的可以不设)。具体的可以参照简单的下面给出的一个例子。 再有一点要注意的就是,你要把你的统计类型文件的后缀添加到configuration.ini文 件里去,如里这个文件里没有声明统计这个类型;另外你还要在config.ini文件里把文 件后缀--统计类 对写好。这样才能正确显示结果 例如:统计.properties文件 首先写一个统计类,假设这个类名为CountProperties,这个类在包count下。我们定 义.properties文件的注释是只有当行首的字符为“#”时认为是一个注释,否则如果行空 则认为是一个空行,其他的则认为是一个代码行。则代码如下: package count; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import basecount.BaseCount; import result.CountResult; public class CountPropertiesFile extends BaseCount { /** * implements this method to custom */ public void countTypeFile(File sourcefile, CountResult result) { countPropertiesFile(sourcefile, result); } /** * this method used to count properties file * the comment signal is "#" * @param sourcefile * @param result */ private void countPropertiesFile(File sourcefile, CountResult result) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(sourcefile)); String line = null; String trimed = null; while ((line = reader.readLine()) != null) { int i = result.getTotlalLineCount(); result.setTotlalLineCount(++i); trimed = line.trim(); if (trimed.length() < 1) { i = result.getBlankLineCount(); result.setBlankLineCount(++i); } else if (trimed.startsWith("#")) { i = result.getCommentLineCount(); result.setCommentLineCount(++i); } else { i = result.getCodeLineCount(); result.setCodeLineCount(++i); } } } catch (Exception ex) { ex.printStackTrace(); } finally { try { if(reader!=null) reader.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } 写完这个代码以后,在config.ini文件里增加一行,内容如下: .properties=count.CountPropertiesFile 如果configuration.ini里的可统计类型不包括.properties文件,则增加以下内容 SourceFilter.countFileExtensions=.../.properties 这就完成了增加自定义的统计.properties类型文件的目的了!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值