当前位置:编程学习 > JAVA >>

Java统计代码的小工具

 

统计代码的小工具,程序功能不是很完善,欢迎大家多给意见

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 javax.swing.JFileChooser; 

import javax.swing.JFrame; 

import javax.swing.JMenu; 

import javax.swing.JMenuBar; 

import javax.swing.JMenuItem; 

import javax.swing.JScrollPane; 

import javax.swing.JTextArea; 

import javax.swing.UIManager; 

 

public class CodeCounterFrame extends JFrame 

    JMenuBar menuBar = new JMenuBar(); 

    JMenu fileMenu = new JMenu("文件"); 

    JMenuItem openMenuItem = new JMenuItem("打开"); 

    JTextArea txa = new JTextArea(); 

    JScrollPane jsp =new JScrollPane(txa); 

    String output = ""; 

    static long codeLines = 0; 

    static long commentLines = 0; 

    static long blankLines = 0; 

    public CodeCounterFrame() 

    { 

        this.setJMenuBar(menuBar); 

        menuBar.add(fileMenu); 

        fileMenu.add(openMenuItem); 

        txa.setEditable(false); //txa里面的内容不能被编辑 

        add(jsp); 

         

        //内部匿名类易做图 

        openMenuItem.addActionListener(new ActionListener() 

        { 

            public void actionPerformed(ActionEvent e) 

            { 

                if(e.getSource() == openMenuItem) 

                { 

                    //点击了打开菜单 

                    //弹出文件选择器 

                    JFileChooser chooser = new JFileChooser(); 

                    chooser.showOpenDialog(null); 

                     

                    //获取选中的文件 

                    File selectedFile = chooser.getSelectedFile(); 

                    BufferedReader br = null; 

                    boolean flag = false; 

                    try { 

                         FileReader reader = new FileReader(selectedFile);//读取文档的内容 

                         br = new BufferedReader(reader); 

                         String line = ""; 

                          

                        //2读文件 

                        while ((line = br.readLine()) != null) 

                        { 

                            line = line.trim(); // 除去注释前的空格 

                            if (line.matches("^[ ]*$"))  

                            {  

                                // 匹配空行 

                                blankLines++; 

                            }  

                            else if (line.startsWith("//"))  

                            { 

                                commentLines++; 

                            }  

                              else if (line.startsWith("/*") && !line.endsWith("*/")) 

            

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,