统计一个项目的有效代码

package com.imti.work.cla;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Rectangle;
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 javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**统计一个项目中代码的行数,空行,注释数。*/
public class ProjectCountTools extends JFrame implements ActionListener
{
 private static final long serialVersionUID = -8565575686696452521L;
 //真正的有效代码行要过滤以下
 public final String[] filters =
 {
   "/",
   "*",
   "package",
   "import"
 };
 //文件夹位置。
 private String path = "";
 //文件夹下所有的以.java结尾的文件。
 private ArrayList<String> fileNames = null;
 //打开项目所在文件夹
 private Button openProject = new Button("选择项目");
 //显示结果。
 private JTextArea textarea = new JTextArea(15,50);

 private JScrollPane jsp = null;
 
 //有效代码行数。
 private int youxiao = 0;
 //空行
 private int kong = 0;
 //注释行
 private int zhushi = 0;
 //import行
 private int importhang = 0;
 //package行
 private int packagehang = 0;
 
 /**构造方法摘要*/
 public ProjectCountTools()
 {
  this.setLayout(new BorderLayout());
  this.setTitle("项目代码行统计工具");
  this.setBounds(new Rectangle(500,400));
  this.setResizable(false);
  this.setVisible(true);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.add(openProject,BorderLayout.NORTH);
  jsp = new JScrollPane(textarea);
  this.add(jsp,BorderLayout.CENTER);
  openProject.addActionListener(this);
  textarea.setWrapStyleWord(true);
  this.pack();
 }
 /**
  * @param args
  */
 public static void main(String[] args)
 {
  new ProjectCountTools();
  
 }
 
 /**事件处理方法*/
 public void actionPerformed(ActionEvent event)
 {
  if(event.getSource().equals(openProject))
  {
   //FileDialog dialog = new FileDialog(this);
   //dialog.setVisible(true);
   JFileChooser filechooser = new JFileChooser();
   filechooser.setDialogType(JFileChooser.DIRECTORIES_ONLY);
   filechooser.setDialogTitle("选择一个项目");
   filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
   int res = filechooser.showOpenDialog(this);
   if(res == JFileChooser.APPROVE_OPTION)
   {
    path = filechooser.getSelectedFile().getAbsolutePath();
    if(!"".equals(path) && null != path)
    {  
      textarea.setText("");
      youxiao = 0;
      kong = 0;
      zhushi = 0;
      
      fileNames = new ArrayList<String>();
      
      textarea.append("项目位置:"+path+"/n包含以下java文件:/n");
      getFiles(path);
      for(int i = 0;i < fileNames.size();i++)
      {
       textarea.append(fileNames.get(i)+"/n");
      }
      textarea.append("总共包含"+fileNames.size()+"个java文件/n");
      textarea.append("------------------------分析结果-------------------/n");
      getCountFromFiles(fileNames);
      textarea.append("有效代码行:"+youxiao+"/n");
      textarea.append("空行:"+kong+"/n");
      textarea.append("注释行:"+zhushi+"/n");
      textarea.append("import行:"+importhang+"/n");
      textarea.append("package行:"+packagehang+"/n");
    }
   }
  }
 }
 
 //取得文件夹下所有的以.java结尾的文件。
 public void getFiles(String path)
 {
  File file = new File(path);
  //如果是文件夹。
  if(file.isDirectory())
  {
   File[] files = file.listFiles();
   for(File f :files)
   {
    //如果是文件,保存文件名。
    if(f.isFile() && f.getName().endsWith(".java"))
    {
     fileNames.add(f.getPath());
    }
    else
    {
     getFiles(f.getAbsolutePath());
    }
   }
  }
 }
 
 /**分析文件中所包含的有效代码行,空行,注释的数目。*/
 public void getCountFromFiles(ArrayList<String> fileNames)
 {
  for (int i = 0; i < fileNames.size(); i++)
  {
   //完整文件名。
   String fileName = fileNames.get(i);
   BufferedReader br = null;
   try
   {
     br = new BufferedReader(new FileReader(fileName));
     String temp = null;
     while((temp = br.readLine()) != null)
     {
      temp = temp.trim();
      if(temp.startsWith("*")||temp.startsWith("/"))
      {
       zhushi++;
      }
      else if(isKong(temp))
      {
       kong++;
      }
      else if((temp.startsWith("import")))
      {
       importhang++;
      }
      else if((temp.startsWith("package")))
      {
       packagehang++;
      }
      else
      {
       youxiao++;
      }
     }
   } catch (FileNotFoundException e)
   {
    e.printStackTrace();
   } catch (IOException e)
   {
    e.printStackTrace();
   }
   finally
   {
    try
    {
     br.close();
    } catch (IOException e)
    {
     e.printStackTrace();
    }
   }
  }
 }
 
 /**判断某一个字符串是否为空行。*/
 public boolean isKong(String str)
 {
  for (int i = 0; i < str.length(); i++)
  {
   if(!(str.charAt(i) == ' '))
   {
    return false;
   }
  }
  return true;
 }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值