Java 综合性实验 Java源代码分析程序
題目
Java課程的綜合實驗…大三的時候寫過的,不過現在回頭看,發現寫得真爛,所以在學習Java過程中重構了代碼.
基本不算重構而是重寫…改的時候差點看不懂自己寫過什么…好了言歸正傳:
實驗的要求如下:
一、題目:綜合性實驗 Java源代碼分析程序
二、類型:綜合型、探索型
三、目的:初步掌握運用面向對象方法編寫應用程序,掌握類、對象、封裝等;了解并應用Java語言的字符串處理、文本文件的讀寫
四、內容:
1.背景描述:
(1)Java語言共有50個關鍵字。
(2)Java源程序是以“.java”為擴展名的文本文件。
(3)可以考慮在Java源程序中的行共有3種:
代碼行,可運行的Java源代碼。例如:
int n = 10;
注釋行,3種注釋均可。例如:
/**
文檔注釋
*/
/*
多行注釋
/
//單行注釋
空行,既無代碼,也無注釋;
(4)特殊行的處理方法
如果有以下行尾單行注釋的情況,將該行判定為代碼行。
int number; //number表示人數
int n; /n表示數量/
如果有以下行尾多行注釋的情況,第1行判定為代碼行,第二行判定為注釋行。
int number; / number為整型
表示人數 */
假設被分析程序源碼無其他特殊情況,如:
int /人數/ number;
2. 項目名和類名為JavaCodeAnalyzer,主類名等其他類名自行定義。
實現功能:
(1) 程序運行時要求輸入一個目錄的名稱。目錄不存在或不是目錄要提示錯誤并重新輸入。
(2) 找出輸入目錄中所有的Java源程序文件(文件擴展名為“.java”), 并進行源程序文件進行分析。
需要分析的結果有:
目錄中源程序文件個數
所有源程序文件總的字節數
所有源程序文件中代碼行數、注釋行數、空行數及總行數。說明:行以回車結束。
(3) 統計并按從多到少輸出所有源程序文件中使用的Java關鍵字及其出現次數。
(4) 統計分析的結果除在屏幕顯示外,還需要存儲到一個文本文件中,文件內容應該如下:
目錄名稱:XXXXX(絕對路徑方式)
共有源程序文件XX個,總的大小為:XXXXXX 字節
源程序文件總行數:xxxx,其中:
代碼行數:xxx,占xx.xx%
注釋行數:xxx,占xx.xx%
空白行數:xxx,占xx.xx%
源程序文件中使用過的關鍵字有(按使用次數降序排列):
關鍵字1:xx次
關鍵字2:xx次
關鍵字3:xx次
關鍵字4:xx次
關鍵字5:xx次
本次分析時間:年-月-日,時-分-秒
注意:統計關鍵字使用次數時,要排除注釋中出現的關鍵字和字符串直接量中出現的關鍵字。
好了,總的來說可以分成以下幾個模塊:
1.判斷輸入目錄是否正確
2.搜索Java文件并保存到ArrayList
3.計算Java文件個數和總大小
4.統計源文件的代碼行數,注釋行數等
5.統計源文件的關鍵字出現次數
6.將輸出結果保存到文件
7.輸出計算用時
其中
2,3可以參考我的文章 [Java]統計指定目錄中文件的個數和總的大小
4可以參考[Java]統計Java源文件代碼行數,注釋行數,空白行數
5可以參考 [Java]統計目錄下Java源文件的關鍵字出現次數
代碼實現
※重構代碼中對6,7不做實現
1.判斷輸入目錄是否正確
public static String getPathName() {System.out.println("輸入目錄路徑:");Scanner keyboardInput = new Scanner(System.in);String pathName = null;File root = null;while (true) {pathName = keyboardInput.nextLine();root = new File(pathName);if (root.isFile()) {System.out.println("輸入不是目錄,請重新輸入");} else if (!root.exists()) {System.out.println("目錄不存在,請重新輸入");} else {break;}}return pathName;}2.搜索Java文件并保存到ArrayList
public void searchFiles() {File[] files = root.listFiles();int length = files.length;for (int i = 0; i < length; i++) {if (files[i].isDirectory()) {root = files[i];searchFiles();} else {if (files[i].getName().endsWith(".java"))fileList.add(files[i]);}}}3.計算Java文件個數和總大小
public void countFiles() {long totalSize = 0;System.out.println("目錄名稱:" + fileList.get(0).getParent());System.out.println("文件數:" + fileList.size());for (int i = 0; i < fileList.size(); i++) {totalSize += fileList.get(i).length();}System.out.println("文件總大小(bytes):" + totalSize);}4.統計源文件的代碼行數,注釋行數等
代碼與我的文章[Java]統計Java源文件代碼行數,注釋行數,空白行數一致.這里僅貼代碼,詳細解析請參考文章
import java.io.*; import java.util.ArrayList; import java.text.DecimalFormat;public class CodeAnalyzer {public void codeAnalyze(ArrayList<File> fileList) {double rowsCount = 0;double commentsCount = 0;double blanksCount = 0;double codesCount = 0;DecimalFormat df = new DecimalFormat("#.##");for (File file : fileList) {try {rowsCount += countRows(file);blanksCount += countBlanks(file);commentsCount += countComments(file);codesCount = rowsCount - blanksCount - commentsCount;} catch (IOException e) {e.printStackTrace();}}//輸出結果System.out.println("源程序文件總行數:" + (int) rowsCount);System.out.println("代碼行數:" + (int) codesCount + ",占" + df.format(codesCount / rowsCount * 100) + "%");System.out.println("注釋行數:" + (int) commentsCount + ",占" + df.format(commentsCount / rowsCount * 100) + "%");System.out.println("空白行數:" + (int) blanksCount + ",占" + df.format(blanksCount / rowsCount * 100) + "%");}public int countRows(File file) throws IOException {BufferedReader input = new BufferedReader(new FileReader(file));int rows = 0;while (input.readLine() != null) {rows++;}return rows;}public int countBlanks(File file) throws IOException {BufferedReader input = new BufferedReader(new FileReader(file));int blanks = 0;String line = null;while ((line = input.readLine()) != null) {if (line.trim().equals("")) blanks++;}return blanks;}public int countComments(File file) throws IOException {BufferedReader input = new BufferedReader(new FileReader(file));int comments = 0;String line = null;while ((line = input.readLine()) != null) {line = line.trim();if (line.startsWith("//")) {//單行注釋comments++;} else if (line.startsWith("/*")) { //多行及文檔注釋comments++;while (!line.endsWith("*/")) {line = input.readLine().trim();comments++;}} else if (line.contains("/*")) { //下行尾多行注釋line = input.readLine().trim();if (line.endsWith("*/")) comments++;}}return comments;}}5.統計源文件的關鍵字出現次數
與4一樣,這里僅貼代碼,詳情解析到-> [Java]統計目錄下Java源文件的關鍵字出現次數
import java.io.*; import java.util.*;public class KeywordsAnalyzer {Map keywords;final String[] KEYWORDS = { //50個關鍵字"abstract", "assert", "boolean", "break", "byte","case", "catch", "char", "class", "const","continue", "default", "do", "double", "else","enum", "extends", "final", "finally", "float","for", "goto", "if", "implements", "import","instanceof", "int", "interface", "long", "native","new", "package", "private", "protected", "public","return", "strictfp", "short", "static", "super","switch", "synchronized", "this", "throw", "throws","transient", "try", "void", "volatile", "while"};public KeywordsAnalyzer() {keywords = new HashMap();for (String word : KEYWORDS) {keywords.put(word, 0);}}public void keywordsAnalyze(ArrayList<File> fileList) {for (File file : fileList) {try {countKeyWords(file);} catch (IOException e) {e.printStackTrace();}}//排序并輸出結果List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(keywords.entrySet());Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {@Overridepublic int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {return o2.getValue().compareTo(o1.getValue());}});int count = 0;System.out.println("\n源程序文件中使用過的關鍵字有(按使用次數降序排列):");for(int i=0;i<5;i++){System.out.println("關鍵字 "+list.get(i).getKey()+": "+list.get(i).getValue()+"次");}}public void countKeyWords(File file) throws IOException {BufferedReader input = new BufferedReader(new FileReader(file));String line = null;while ((line = input.readLine()) != null) {line = line.trim();if (line.startsWith("//")) continue; //不處理單行注釋else if (line.contains("/*")) { //多行,文檔與尾行注釋if (!line.startsWith("/*")) matchKeywords(line);//第一行算代碼,其余算注釋while (!line.endsWith("*/")) {line = input.readLine().trim();}}matchKeywords(line); //對代碼行進行統計}}public void matchKeywords(String line) {String[] wordList = line.replaceAll("\\W", " ").split(" ");for (int i = 0; i < wordList.length; i++) {for (int j = 0; j < 50; j++) {if (wordList[i].equals(KEYWORDS[j])) {int count = (int) keywords.get(KEYWORDS[j]);keywords.put(KEYWORDS[j], count + 1);}}}} }測試結果
測試Timer.java,得到結果:
總結
以上是生活随笔為你收集整理的Java 综合性实验 Java源代码分析程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: csapp-深入理解计算机系统学习记录
- 下一篇: 前端学习(1849)vue之电商管理系统