统计JAVA代码


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class CountCodes extends JFrame{
 //新增
  private long allCode = 0;
  private long allComment = 0;
  private long allBlank = 0;
 
 
    private long codeLines = 0;
    private long commentLines = 0;
    private long blankLines = 0;
    private File fileName;
    
    private JLabel pathLabel;
    private JTextField pathField;
    private JButton openButton;
    private JPanel panelNorth, panelSouth;

    private ArrayList<File> list = new ArrayList<File> ();
    private String[] tableHeader = {"文件名", "JAVA代码行数", "注释行数", "空行"};
    private Vector<String> vectorHeader = new Vector<String> ();
    private Vector<Vector> values = new Vector<Vector> ();
    private JTable table;
    private DefaultTableModel tableModel;
    private JScrollPane scrollPane;
    
    public CountCodes(){
        super("Count Codes");
        
        pathLabel = new JLabel("文件路径: ");
        pathField = new JTextField(100);
        pathField.setEditable(false);
        openButton = new JButton("打开");
        
        openButton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        openFile();
                        
                        if (fileName == null)
                            return;
                        
                        analysisFileOrDir(fileName);
                        
                        for(File file : list){
                            analyze(file);
                        }   
                      //新增 
                      System.out.println(allCode);
                      System.out.println(allComment);
                      System.out.println(allBlank);
                      System.out.println("-------------------");
                      Vector<String> value = new Vector<String> ();
                      value.add("总计");
                      value.add(String.valueOf(allCode));
                      value.add(String.valueOf(allComment));
                      value.add(String.valueOf(allBlank));  
                      
                      values.add(value);
                       
                    }
                });
        panelNorth = new JPanel();
        
        panelNorth.setLayout(new BorderLayout());
        panelNorth.add(pathLabel, BorderLayout.WEST);
        panelNorth.add(pathField, BorderLayout.CENTER);
        panelNorth.add(openButton, BorderLayout.EAST);
        
        for (int i=0; i<tableHeader.length; i++)
            vectorHeader.add(tableHeader[i]);
       
       
       
       
        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);
        
        setSize(450, 400);
        setVisible(true);
    }
    
    public static void main(String args[]){
        CountCodes countCodes = new CountCodes();
        countCodes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void openFile(){
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        
        int result = fileChooser.showOpenDialog(this);
        
        if (result == JFileChooser.CANCEL_OPTION)
            return;
        
        fileName = fileChooser.getSelectedFile();
        
        if (fileName == null || fileName.getName().equals(""))
            JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
        
        pathField.setText(fileName.toString());
    }
    private void zeroVariables(){
        this.codeLines = 0;
        this.commentLines = 0;
        this.blankLines = 0;
    }
    private void analysisFileOrDir(File fileName){
        if(fileName.isFile()){
            list.add(fileName);
            return;
        }
        else{
            File[] fileArray = fileName.listFiles();
            
            for(File inArray : fileArray){
                if(inArray.isFile() && inArray.getName().matches(".*\\.java$")){
                    list.add(inArray);
                }
                else if(inArray.isDirectory()){
                    analysisFileOrDir(inArray);
                }
            }
        }
    }
    
    private void analyze(File fileName){
        Vector<String> value = new Vector<String> ();
        
        String[] strArray = fileName.toString().split("\\\\");
        
       // value.add(strArray[strArray.length-1]);
        value.add(fileName.toString());
        BufferedReader br = null;
        boolean comment = false;
        try {
            br = new BufferedReader(new FileReader(fileName));
            String line = "";
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.matches("^[\\s&&[^\\n]]*$")) {
                    blankLines++;
                } else if (line.startsWith("/*") && !line.endsWith("*/")) {
                    commentLines++;
                    comment = true;
                } else if (line.startsWith("/*") && line.endsWith("*/")) {
                    commentLines++;
                } else if (true == comment) {
                    commentLines++;
                    if (line.endsWith("*/")) {
                        comment = false;
                    }
                } else if (line.startsWith("//")) {
                    commentLines++;
                } else {
                    codeLines++;
                }
            }
            
            value.add(String.valueOf(codeLines));
            value.add(String.valueOf(commentLines));
            value.add(String.valueOf(blankLines));  
            
            values.add(value);
           
           
            this.table.revalidate();
            allCode+=codeLines;
            allComment+=commentLines;
            allBlank+=blankLines;
           
           
//            System.out.println(all1);
//            System.out.println(all2);
//            System.out.println(all3);
//            System.out.println("-------------------");
           
            zeroVariables();
           
          
           
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                    br = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值