网上看到的一个统计代码行工具

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);    
    }  

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还,也可在此程序基础上进修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
1、资源项目源码均已通过严格测试验证,保证能够正常运; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值