⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 read.java

📁 查询项目文件个数已经代码行数。分别统计各类文件的平均行数。
💻 JAVA
字号:
package com.cn;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Read {
	static int txtCount = 0;
	static int groovyCount = 0;
	static int htmlCount = 0;
	static int xmlCount = 0;
	static int jsCount = 0;
	static int txtCodeTotal = 0;
	static int groovyCodeTotal = 0;
	static int htmlCodeTotal = 0;
	static int xmlCodeTotal = 0;
	static int jsCodeTotal = 0;
	static Map<String, String> map = new HashMap<String, String>();

	public boolean readFile(String filePath) throws Exception {

		File file = new File(filePath);
		if (!file.isDirectory()) {
			getData(file.getName(), file.getAbsolutePath());
		} else if (file.isDirectory()) {
			String[] fileList = file.list();
			for (int i = 0; i < fileList.length; i++) {
				File readFile = new File(filePath + "\\" + fileList[i]);
				if (!readFile.isDirectory()) {
					if (map.containsKey(readFile.getName())) {
						continue;
					} else {
						map.put(readFile.getName(), readFile.getName());
						getData(readFile.getName(), readFile.getAbsolutePath());
					}
				} else if (readFile.isDirectory()) {
					readFile(filePath + "\\" + fileList[i]);
				}
			}
		}
		return true;
	}

	public boolean getData(String fileName, String filePath) throws Exception {
		String fileSuffix = null;
		StringTokenizer stringTokenizer = new StringTokenizer(fileName, ".");
		while (stringTokenizer.hasMoreTokens()) {
			fileSuffix = stringTokenizer.nextToken();
		}
		char f = fileSuffix.charAt(0);
		if (fileSuffix.equals("groovy") || fileSuffix.equals("html")
				|| fileSuffix.equals("xml") || fileSuffix.equals("js")
				|| fileSuffix.equals("txt")) {
			switch (f) {
			case 't':
				System.out.print("txt文件:=" + "    " + fileName + " ");
				codeNumber(filePath, fileSuffix);
				++txtCount;
				break;
			case 'g':
				System.out.print("groovy文件:=" + "    " + fileName + " ");
				codeNumber(filePath, fileSuffix);
				++groovyCount;
				break;
			case 'h':
				System.out.print("html文件:=" + "    " + fileName + " ");
				codeNumber(filePath, fileSuffix);
				++htmlCount;
				break;
			case 'j':
				System.out.print("js文件:=" + "    " + fileName + " ");
				codeNumber(filePath, fileSuffix);
				++jsCount;
				break;
			case 'x':
				System.out.print("xml文件:=" + "    " + fileName + " ");
				codeNumber(filePath, fileSuffix);
				++xmlCount;
				break;
			}
		} else {
		}
		return true;
	}

	public boolean codeNumber(String filePath, String fileType)
			throws Exception {

		@SuppressWarnings("unused")
		String str;
		int Count = 0;
		int Count1 = 0;
		int Count2 = 0;
		FileReader fileReader = new FileReader(filePath);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		int len = 0;
		while ((str = bufferedReader.readLine()) != null) {
			Count++;
			len = str.trim().length();
			if (len == 0) {
				Count1++;
			}
			if (str.trim().startsWith("//") || str.trim().startsWith("/*")
					|| str.trim().startsWith("*")) {
				Count2++;
			}
		}
		System.out.println("         " + "代码行数:" + (Count - Count1 - Count2));
		if (fileType.equals("txt")) {
			txtCodeTotal = txtCodeTotal + (Count - Count1 - Count2);
		}
		if (fileType.equals("html")) {
			htmlCodeTotal = htmlCodeTotal + (Count - Count1 - Count2);
		}
		if (fileType.equals("js")) {
			jsCodeTotal = jsCodeTotal + (Count - Count1 - Count2);
		}
		if (fileType.equals("groovy")) {
			groovyCodeTotal = groovyCodeTotal + (Count - Count1 - Count2);
		}
		if (fileType.equals("xml")) {
			xmlCodeTotal = xmlCodeTotal + (Count - Count1 - Count2);
		}
		fileReader.close();
		return true;
	}

	public static void main(String[] args) throws Exception {

		Read read = new Read();
		read.readFile("文件路径");
		System.out.println("txt文件个数    :" + txtCount + "   " + "代码总行数:"
				+ txtCodeTotal + "   " + "平均行数" + (txtCodeTotal / txtCount));
		System.out.println("groovy文件个数 :" + groovyCount + "   " + "代码总行数:"
				+ groovyCodeTotal + "   " + "平均行数"
				+ (groovyCodeTotal / groovyCount));
		System.out.println("html文件个数   :" + htmlCount + "   " + "代码总行数:"
				+ htmlCodeTotal + "   " + "平均行数" + (htmlCodeTotal / htmlCount));
		System.out.println("xml文件个数    :" + xmlCount + "   " + "代码总行数:"
				+ xmlCodeTotal + "   " + "平均行数" + (xmlCodeTotal / xmlCount));
		System.out.println("js文件个数     :" + jsCount + "   " + "代码总行数:"
				+ jsCodeTotal + "   " + "平均行数" + (jsCodeTotal / jsCount));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -