package test;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
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 long normalLines=0;
/**注释行数*/
private long commentLines=0;
/**空白行数*/
private long spaceLines=0;
/**总行数*/
private long totalLines=0;
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));
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();
fileField.setText(path);
File file = new File(path);
CalculateLineNum(file);
}
}
});
}
/*
* 计算并显示统计信息
*/
private void CalculateLineNum(File file)
{
if(file.exists())
{
this.setNormalLines(0L);
this.setCommentLines(0L);
this.setSpaceLines(0L);
this.setTotalLines(0L);
displayLineNum(file);
myFormat = (DecimalFormat)NumberFormat.getPercentInstance();
myFormat.applyPattern("0.00%");
totalLines=normalLines+spaceLines+commentLines;
filePathArea.setText("");
filePathArea.setText(filePathArea.getText() + "\n" + " 总行数:" + totalLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 程序行数:" + normalLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 注释行数:" + commentLines + "行");
filePathArea.setText(filePathArea.getText() + "\n" + " 空行行数:" + spaceLines + "行");
}
}
//循环访问目录及子目录,统计代码总行数,注释行数及空行行数
public void displayLineNum(File file)
{
BufferedReader br=null;
boolean comment=false;
try {
br=new BufferedReader(new FileReader(file));
String line="";
while((line=br.readLine())!=null){
line=line.trim();//去除空格
//空白
if(line.matches("^[\\s&&[^\\n]]*$")) {
spaceLines ++;
}
// //
else if(line.startsWith("//")) {
commentLines ++;
}
// 有/*的情况
else if(line.indexOf("/*") != -1)
{
// /*开始的情况
if(line.startsWith("/*"))
{
// 同一行里有*/
if(line.indexOf("*/") != -1)
{
//以*/结尾
if(line.endsWith("*/"))
{
commentLines ++;
}
else
{
normalLines ++;
}
}
else
{
commentLines ++;
comment = true;
}
}
// /*不是开始的情况
else
{
//同一行里没有*/
if(line.indexOf("*/") == -1)
{
comment = true;
normalLines ++;
}
}
}
else if(true == comment)
{
//是否有*/
if(line.indexOf("*/") != -1)
{
if(line.endsWith("*/"))
{
commentLines ++;
}
else
{
normalLines ++;
}
comment = false;
}
else
{
commentLines ++;
}
}
else
{
normalLines ++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
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);
}
public long getNormalLines() {
return normalLines;
}
public void setNormalLines(long normalLines) {
this.normalLines = normalLines;
}
public long getCommentLines() {
return commentLines;
}
public void setCommentLines(long commentLines) {
this.commentLines = commentLines;
}
public long getSpaceLines() {
return spaceLines;
}
public void setSpaceLines(long spaceLines) {
this.spaceLines = spaceLines;
}
public long getTotalLines() {
return totalLines;
}
public void setTotalLines(long totalLines) {
this.totalLines = totalLines;
}
}
分享到:
相关推荐
获取单个文件行数 文件:test1.sh 行数:20 方法一 代码如下: awk ‘{print NR}’ test1.sh|tail -n1 如图所示: 方法二 代码如下: awk ‘END{print NR}’ test1.sh 如图所示: 方法三 代码如下: grep -n “” ...
* modifield time 2015-4-29 增加支持单个文件统计,单个文件,单个目录混合统计,/**\/以及<!---->去重功能重写 * @param $dir 文件的目录,或者是目录数组,或者是文件数组 * @param $stripRegular 要去除注释的...
可以统计代码行数的插件,测试完全,保证正确,可以统计工程目录,单个文件,多个文件java代码中注释,代码行数
QT文件查询,查询单个文件和文件夹下所有文件行数
java 提取单个字符 java 提取单个字符
java代码实现单个或多个文件压缩成rar包,本地要安装winRar插件。
# java实现对文件的各种操作的工具类 ## 可以实现的操作有: 1. 删除单个文件 2. 删除文件夹及文件夹下的文件 3. 使用文件流对单个文件进行复制 4. 复制整个文件夹内容(包含子文件夹中的所有内容) 5. ...
Java代码,根据URL方式下载单个文件或者图片,根据文件大小进行分批启动多线程下载!
用途: 用来统计记事本里文本的行数。(如果记事本中有空行将不计入统计。单个记事本有百万行数据请至少等待10秒以上) 软件安全无毒,自己亲自编译。卡巴KIS2010 360 检查无毒。
web项目基于Response对象实现。
可以统计单个文件,整个文件夹,整张磁盘的各种文件行数。 有兴趣的可以下载看看
Java计算文件MD5值(支持大文件) package com.hthl.xxtd; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.MessageDigest; import org....
和那些只能统计单个文件的统计器不同,这款统计器能够接受文件夹路径,批量统计整个文件夹的所有文件,包括子文件夹中的有效文件。这样可以大大的提高统计效率,节约用户的时间,使编程人员能集中更多精力编写程序。...
java 文件zip解压缩,单个文件以及多个文件,有需要的请下载。
这是本人自己开发的一款java代码注释统计工具,支持统计总行数,总代码行数,总注释行数,注释率,注释合格率自定义,统计详细,单个文件统计情况,可导出统计报告等! 注:本软件未捆绑jre(java环境),需要在已...
程序测试可用,直接解压导入到工程就可以,bat文件跟shell文件是用于在window跟linux上直接执行的脚本 我把开发的配置文档附上: 1.程序为定时任务,任务执行时间在bin目录下的配置文件mergeFilleUtil.properties中...
文件上传下载工具类单个文件上传,多种传参形式下载文件
可以统计单个文件的代码行数,或者整个工程的代码行数。 不需安装,即下即用。 支持C,C++,BASIC,DELPHI,JAVA,ASM多种语言。 项目管理者、测试人员必用工具。