(2008年4月25日23:43)
最近在学java,写了一些代码,想统计自己到底写了多少代码,但统计起来比较烦琐,于是想写个程序方便以后统计。
这个小程序能够统计某目录及其子目录下java文件代码行数,注释行数,空格行数以及各自所占百分比。
源程序如下:
package linenum;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import javax.swing.*;
public class LineNum extends JFrame
{
private JPanel topPanel = new JPanel();
private JPanel bottomPanel = new JPanel();
private JButton fileChoose = new JButton("选择目录");
private JTextField fileField = new JTextField(20);
private JFileChooser fc = new JFileChooser("选择目录");
private JTextArea filePathArea = new JTextArea(5 , 20);
//判断是否属于"/* */注释"
private boolean isExplainStatus = false;
//存储代码总行数值
private int totalCount = 0;
//存储注释总行数值
private int explainCount = 0;
//存储空行总行数值
private int spaceCount = 0;
//存储单个文件行数值
private int count = 0;
private InputStream input = null;
private BufferedReader br = null;
private String totalPath = "";
private DecimalFormat myFormat = null;
public LineNum(String title)
{
super(title);
//设置面板
Container container = getContentPane();
container.setLayout(new BorderLayout());
topPanel.setLayout(new GridLayout(1 , 2));
bottomPanel.setLayout(new BorderLayout());
topPanel.add(fileChoose);
topPanel.add(fileField);
bottomPanel.add(new JScrollPane(filePathArea));
filePathArea.setText("java文件:");
container.add(topPanel , BorderLayout.NORTH);
container.add(bottomPanel , BorderLayout.CENTER);
//添加选择目录监听,默认获取的是选择文件所在的父目录,程序统计对象是此父目录及其子目录下的所有java文件
fileChoose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int result = fc.showOpenDialog(LineNum.this);
if(result == JFileChooser.APPROVE_OPTION)
{
String path = fc.getSelectedFile().getAbsolutePath();
path = path.substring(0 , path.lastIndexOf("\\"));
fileField.setText(path);
File file = new File(path);
CalculateLineNum(file);
}
}
});
}
/*
* 计算并显示统计信息
*/
private void CalculateLineNum(File file)
{
if(file.exists())
{
displayLineNum(file);
myFormat = (DecimalFormat)NumberFormat.getPercentInstance();
myFormat.applyPattern("0.00%");
if(totalCount != 0)
{
double programPercent = (double)(totalCount - explainCount - spaceCount)/(double)totalCount;
double explainPercent = (double)explainCount/(double)totalCount;
double spacePercent = (double)spaceCount/(double)totalCount;
filePathArea.setText(filePathArea.getText() + "\n" + " 总行数:" + totalCount + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 程序行数:" + (totalCount - explainCount - spaceCount) + "行,百分比:"+myFormat.format(programPercent));
filePathArea.setText(filePathArea.getText() + "\n" + " 注释行数:" + explainCount + "行,百分比:"+myFormat.format(explainPercent));
filePathArea.setText(filePathArea.getText() + "\n" + " 空行行数:" + spaceCount + "行,百分比:"+myFormat.format(spacePercent));
}
else
{
filePathArea.setText(filePathArea.getText() + "\n" + " 总行数:" + totalCount + "行");
}
}
}
//循环访问目录及子目录,统计代码总行数,注释行数及空行行数
public void displayLineNum(File file)
{
totalPath+= " || " + file.getName();
String[] subPaths = file.list();
if(subPaths.length == 0)
{
totalPath = totalPath.substring(0 , totalPath.lastIndexOf(" || "));
return;
}
//循环对子目录进行访问计算行数处理
for(int i = 0 ; i < subPaths.length ; i++)
{
count = 0;
File subFile = new File(file.getAbsolutePath() + "\\" + subPaths[i]);
if(subFile.isFile())
{
String subFilePath = subFile.getAbsolutePath();
String extendName = subFilePath.substring(subFilePath.lastIndexOf(".") + 1 , subFilePath.length());
if(!extendName.equals("java"))
{
continue;
}
try
{
input = new FileInputStream(subFile);
BufferedReader br = new BufferedReader(new InputStreamReader(input));
String lineValue = br.readLine();
while(lineValue != null)
{
count++;
//对两种不同类型注释分别处理,对空行用空字符串来判断
if(isExplainStatus == false)
{
if(lineValue.trim().startsWith("//"))
{
explainCount++;
}
if(lineValue.trim().equals(""))
{
spaceCount++;
}
if(lineValue.trim().startsWith("/*"))
{
explainCount++;
isExplainStatus = true;
}
}
else
{
explainCount++;
if(lineValue.trim().startsWith("*/"))
{
isExplainStatus = false;
}
}
lineValue = br.readLine();
}
totalCount+= count;
String totalPath1 = totalPath + " || " + subFile.getName();
//显示单个文件的行数
filePathArea.setText(filePathArea.getText() + "\n" + totalPath1 + " 行数:" + count + "行--------totalCount:"+ totalCount);
br.close();
input.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
//循环调用displayLineNum函数,实现统计子目录行数数据
displayLineNum(subFile);
}
}
totalPath = totalPath.substring(0 , totalPath.lastIndexOf(" || "));
}
public static void main(String args[])
{
LineNum lineFrame=new LineNum("java程序行数统计");
lineFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lineFrame.setBounds(212,159,600,420);
lineFrame.setVisible(true);
lineFrame.setResizable(false);
}
}
分享到:
相关推荐
用于统计项目的代码行数。在代码中修改路径即可。适用于多种文件格式。
运行环境,windows,无需任何配置,解压后双击run.bat即可运行。
支持 js java .net asp html .c vb sql 所有源码文件的代码行数统计,可统计文件夹内文件的代码行数。一键统计,快捷方便
本软件用于统计软件工程源代码行数,可对指定的子目录下或整个目录树中所有指定类型的源代码文件进行行数统计。 本软件的统计结果包含源代码中的注释行和空行,因为作者认为它们同样也是源代码的必要组成部分。 ...
MyEclipse中有一项代码统计的功能,可以统计出代码的行数,但是只能查看Java代码的行数,而Eclipse中根本没有此功能(可能本人没有找到),所以我写了一个代码统计的代码,可以查看工程中的Java代码,jsp代码,xml...
可以统计代码行数的插件,测试完全,保证正确,可以统计工程目录,单个文件,多个文件java代码中注释,代码行数
能统计编写了多少行Java代码和注释,和类的个数
10、支持Java文件 .java 的统计。 11、支持SQL文件 .sql 的统计。 12、支持 *.* 文件类型的自动分析统计。 13、支持文件目录的选择统计方式。 14、支持用户自定义文件统计类型及统计方法。 15、支持Htm,Excel,Csv,...
可以计算jsp,c,js,java等主流文件代码行数,计算准确,容易使用
一个功能十分强大的java版代码行数统计程序,可以用来统计.java .htm .html .jsp .xml等格式的文件的代码行数。如有需要,可以对本程序稍加扩展,以支持其他格式文件(如.css .js .php等)的代码统计
Java代码量统计小工具 1 可按JavaDoc按作者统计代码量 2 统计代码行数、注释行数、空白行、注释率等 3 按某目录及其子目录内.java文件统计 4 需要JRE
可以统计一个目录下的所有指定格式文件的代码行数,用于软件度量的实验.
代码行统计工具用Java语言实现,可以对C\C++、Java的源代码进行统计,统计结果包括:总行数、空行数、注释行数和代码行数,统计结果可以以表格的形式显示给用户或另存为文件。 一 执行环境: 操作系统:WindowsNT/...
获取上G的文件行数的最快速的java代码 占用内存少,而且最快的获取海量文件的java代码
一个统计Java代码的小工具,用于统计你写的Java代码,可以知道你写了多少行代码
代码行数统计器,可以轻松统计出指定文件的代码行数,同时可以删除注册和空白行
一个小小的application,网上有重复
Java统计代码文本文件的字符行数,程序名称:java代码行数统计软件. * 功能:统计代码的行数 * 说明:支持 整个工程的代码行数统计 共有 21 个类 忽略注释忽略空行共 1224 行 包括注释忽略空行共 1370 行 ...
统计.java文件里所有的代码行数,注释行数