java-example:total-line-number
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java-example:total-line-number [2021/11/15 07:40] – morgan0329 | java-example:total-line-number [2021/11/15 07:53] (current) – morgan0329 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== 统计代码行数 ====== | ||
+ | <code java> | ||
+ | package com.platform; | ||
+ | /** | ||
+ | * 用来统计某个文件夹下到底有多少行代码的 | ||
+ | * @author Morgan.L | ||
+ | * @version 1.0 | ||
+ | * @date 2020/10/26 16:31 | ||
+ | */ | ||
+ | |||
+ | import java.io.BufferedReader; | ||
+ | import java.io.File; | ||
+ | import java.io.FileReader; | ||
+ | import java.io.IOException; | ||
+ | import org.apache.commons.lang3.StringUtils; | ||
+ | |||
+ | public class MainTotalLineNumber { | ||
+ | |||
+ | public static void main(String[] args) throws IOException { | ||
+ | countLine(" | ||
+ | System.out.println(" | ||
+ | + " lineNum/ | ||
+ | |||
+ | totalLineNum = 0; | ||
+ | totalFileNum = 0; | ||
+ | countLine(" | ||
+ | System.out.println(" | ||
+ | + " lineNum/ | ||
+ | } | ||
+ | |||
+ | private static BufferedReader bufferedReader = null; | ||
+ | private static int totalLineNum = 0; | ||
+ | private static int totalFileNum = 0; | ||
+ | private static String[] allowFileTypes = {" | ||
+ | |||
+ | private static void countLine(String filePath, boolean ignoreBlankLine) throws IOException { | ||
+ | File file = new File(filePath); | ||
+ | countOneFile(file, | ||
+ | } | ||
+ | |||
+ | private static void countOneFile(File file, boolean ignoreBlankLine) throws IOException { | ||
+ | File[] children = file.listFiles(); | ||
+ | if(children == null) return; | ||
+ | |||
+ | for (int i = 0; i < children.length; | ||
+ | if(children[i].isDirectory() && " | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | if (children[i].isFile() && validFileType(children[i].getName(), | ||
+ | totalFileNum++; | ||
+ | |||
+ | bufferedReader = new BufferedReader(new FileReader(children[i])); | ||
+ | if (ignoreBlankLine) { | ||
+ | String line; | ||
+ | while ((line = bufferedReader.readLine()) != null) { | ||
+ | if (StringUtils.isNotBlank(line)) { | ||
+ | totalLineNum++; | ||
+ | } | ||
+ | } | ||
+ | } else { | ||
+ | while (bufferedReader.readLine() != null) { | ||
+ | totalLineNum++; | ||
+ | } | ||
+ | } | ||
+ | } else { | ||
+ | countOneFile(children[i], | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public static boolean validFileType(String filePath, String... allowTypes) { | ||
+ | if (StringUtils.isNotBlank(filePath)) { | ||
+ | for (String type : allowTypes) { | ||
+ | if (filePath.indexOf(type) > -1) { | ||
+ | return true; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | </ |