统计代码行数(不包括空行)
统计代码行数(不包括空行)
1、方法-统计某文件的代码行数:
https://blog.csdn.net/u014029165/article/details/44859863
1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.FileNotFoundException; 4 import java.io.FileReader; 5 import java.io.IOException; 6 7 public class StaticsCodeingLine { 8 9 private static int whiteLines = 0; 10 private static int commentLines = 0; 11 private static int normalLines = 0; 12 13 14 /** 15 * @param args 16 */ 17 public static void main(String[] args) { 18 File f = new File("G:\\java\\javav\\TextMulTable\\src\\TextMulTable.java"); 19 sumCode(f); 20 } 21 22 private static void sumCode(File file) { 23 BufferedReader br = null; 24 boolean comment = false; 25 try { 26 br = new BufferedReader(new FileReader(file)); 27 String line = ""; 28 try { 29 while ((line = br.readLine()) != null) { 30 line = line.trim(); 31 if (line.matches("^[\\s&&[^\\n]]*$")) { 32 whiteLines++; 33 } else if (line.startsWith("/*") && !line.endsWith("*/")) { 34 commentLines++; 35 comment = true; 36 } else if (true == comment) { 37 commentLines++; 38 if (line.endsWith("*/")) { 39 comment = false; 40 } 41 } else if (line.startsWith("//")) { 42 commentLines++; 43 } else { 44 normalLines++; 45 } 46 } 47 } catch (IOException e) { 48 e.printStackTrace(); 49 } 50 } catch (FileNotFoundException e) { 51 e.printStackTrace(); 52 } finally { 53 if (br != null) { 54 try { 55 System.out.println("空行数:"+whiteLines); 56 System.out.println("注释行数:"+commentLines); 57 System.out.println("代码行数:"+normalLines); 58 br.close(); 59 br = null; 60 } catch (IOException e) { 61 e.printStackTrace(); 62 } 63 } 64 } 65 } 66 }
2、方法-统计某文件夹下的代码行数(不包括文件夹下的文件夹):
https://blog.csdn.net/u014453898/article/details/79655338
1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.FileNotFoundException; 4 import java.io.FileReader; 5 import java.io.IOException; 6 7 public class StaticsCodeingLine { 8 9 private static int whiteLines = 0; 10 private static int commentLines = 0; 11 private static int normalLines = 0; 12 13 14 /** 15 * @param args 16 */ 17 public static void main(String[] args) { 18 int sum=0; 19 20 String path = "D:\\JAVA"; //要遍历的路径 21 File file = new File(path); //获取其file对象 22 File[] fs = file.listFiles(); //遍历path下的文件和目录,放在File数组中 23 24 for(File f:fs){ //遍历File[]数组 25 if(!f.isDirectory()) //若非目录(即文件),则打印 26 System.out.println("文件名:"+f.getName()); 27 sum +=sumCode(f); 28 } 29 System.out.println("==================="); 30 31 System.out.println("总代码行数:"+sum); 32 33 34 } 35 36 private static int sumCode(File file) { 37 BufferedReader br = null; 38 boolean comment = false; 39 try { 40 br = new BufferedReader(new FileReader(file)); 41 String line = ""; 42 try { 43 while ((line = br.readLine()) != null) { 44 line = line.trim(); 45 if (line.matches("^[\\s&&[^\\n]]*$")) { 46 whiteLines++; 47 } else if (line.startsWith("/*") && !line.endsWith("*/")) { 48 commentLines++; 49 comment = true; 50 } else if (true == comment) { 51 commentLines++; 52 if (line.endsWith("*/")) { 53 comment = false; 54 } 55 } else if (line.startsWith("//")) { 56 commentLines++; 57 } else { 58 normalLines++; 59 } 60 } 61 62 } catch (IOException e) { 63 e.printStackTrace(); 64 } 65 } catch (FileNotFoundException e) { 66 e.printStackTrace(); 67 } finally { 68 if (br != null) { 69 try { 70 System.out.println("空行数:"+whiteLines); 71 System.out.println("注释行数:"+commentLines); 72 System.out.println("代码行数:"+normalLines); 73 br.close(); 74 br = null; 75 } catch (IOException e) { 76 e.printStackTrace(); 77 } 78 } 79 } 80 return commentLines; 81 } 82 }
※遍历本目录下所有的文件(包括目录的目录里的文件)
1 import java.io.File; 2 import java.io.FileFilter; 3 4 public class FileText { 5 public static void main(String[] args) { 6 String path = "D:\\JAVA"; //要遍历的路径 7 File file = new File(path); //获取其file对象 8 func(file); 9 } 10 11 private static void func(File file){ 12 File[] fs = file.listFiles(); 13 for(File f:fs){ 14 if(f.isDirectory()) //若是目录,则递归打印该目录下的文件 15 func(f); 16 if(f.isFile()) //若是文件,直接打印 17 System.out.println(f); 18 } 19 } 20 }
posted on 2018-09-10 11:30 helloJava小白 阅读(507) 评论(0) 收藏 举报
浙公网安备 33010602011771号