指定一个.java文件,输出其代码行数

CountList2.java//用来统计java代码行数

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CountList2 {
int countSum;//代码行数

public int getFile(String filePath) throws FileNotFoundException {
  try {
   File file = new File(filePath);
   if (file.isDirectory()) {//判断是否是文件夹
    File[] codeFiles = file.listFiles();
    int s = codeFiles.length;
    for (int i = 0; i < s; i++) {
     getFile(codeFiles[i].getPath());
    }
   } else {
    if (file.getName().endsWith(".java")) {//判断是否是java文件
     System.out.println(file.getName());//输出java文件名称
     countSum += fileList(file);
    }
   }
  } catch (FileNotFoundException e4) {
   System.out.println("erroe1");
   e4.printStackTrace();
  } catch (@SuppressWarnings("hiding") IOException e1) {
   System.out.println("erroe2");
   e1.printStackTrace();
  }
  return countSum;
}

public int fileList(File file) {
  int countSum = 0;//代码行数
  BufferedReader br = null;
  try {
   br = new BufferedReader(new FileReader(file));
   String line = null;
   while ((line = br.readLine()) != null) {
    line = line.trim();
    if (line.matches("^[\\s&&[^\\n]]*$")) {
     blankSum++;
    } else if (line.startsWith("//")) {
     commentSum++;
    } else if (line.startsWith("/*")) {
     commentSum++;
    } else if (line.startsWith("*")) {
     commentSum++;
    } else if (line.endsWith("*/")) {
     commentSum++;
    } else {
     countSum++;
    }
   }
   br.close();
  } catch (Exception e) {
   System.out.println("erroe3");
   e.printStackTrace();
  }
  return countSum;
}

}


countList1.java//用来测试

import java.io.FileNotFoundException;

public class countList1 {

public static void main(String[] args) throws FileNotFoundException {
  String filePath = "E:\\nbtserver";
  CountList2 fn = new CountList2();
  int countSum1=fn.getFile(filePath);
  System.out.println("countSum: " + countSum1);
}
}


<img src="https://img-blog.csdn.net/20150402112440991?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemVuZ3FpY2hlbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
以下是一个Java程序,可以统计一个文件夹中所有文件代码行数(包括空白行和注释): ```java import java.io.*; public class CodeCounter { private static long codeLines = 0; // 代码行数 private static long commentLines = 0; // 注释行数 private static long blankLines = 0; // 空白行数 public static void main(String[] args) { String folderPath = "路径/文件夹"; // 文件夹路径 File folder = new File(folderPath); if (!folder.isDirectory()) { System.out.println("请指定文件夹路径!"); return; } countFiles(folder); System.out.println("代码行数:" + codeLines); System.out.println("注释行数:" + commentLines); System.out.println("空白行数:" + blankLines); } private static void countFiles(File folder) { File[] files = folder.listFiles(); for (File file : files) { if (file.isDirectory()) { countFiles(file); } else { countFile(file); } } } private static void countFile(File file) { BufferedReader reader = null; boolean isComment = false; // 是否在注释中 try { reader = new BufferedReader(new FileReader(file)); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (line.startsWith("/*")) { // 多行注释开始 isComment = true; commentLines++; } else if (line.startsWith("*")) { // 多行注释中 commentLines++; } else if (line.startsWith("*/")) { // 多行注释结束 isComment = false; commentLines++; } else if (line.startsWith("//")) { // 单行注释 commentLines++; } else if (isComment) { // 多行注释中 commentLines++; } else if (line.length() == 0) { // 空白行 blankLines++; } else { // 代码行 codeLines++; } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } } } ``` 使用时,只需要修改`folderPath`变量为需要统计的文件夹路径即可。程序会递归遍历所有子文件夹和文件,统计它们的代码行数、注释行数和空白行数,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值