java實現WC項目
阿新 • • 發佈:2018-09-14
odin 同事 空指針 font directory 要求 reporting 操作 正則表達式
個人項目:WC
wc.exe 是一個常見的工具,它能統計文本文件的字符數、單詞數和行數。這個項目要求寫一個命令行程序,模仿已有wc.exe 的功能,並加以擴充,給出某程序設計語言源文件的字符數、單詞數和行數。
GitHub地址:https://github.com/lllhhhyyy/mygit
基本功能列表
- -c [文件名] 返回文件的字符數(實現)
- -w [文件名] 返回文件詞的數目(實現)
- -l [文件名] 返回文件的行數(實現)
擴展功能列表
- -s 遞歸處理目錄下符合條件的文件。(實現)
- -a 返回更復雜的數據(代碼行 / 空行 / 註釋行)(實現)
- .處理通配符(未實現)
高級功能列表
1. -x顯示圖形界面(未實現)
PSP
PSP2.1 | Personal Software Process Stages | 預估耗時(分鐘) | 實際耗時(分鐘) |
---|---|---|---|
Planning | 計劃 | 30 | 60 |
· Estimate | · 估計這個任務需要多少時間 | 240 | 300 |
Development | 開發 | 60 | 120 |
· Analysis | · 需求分析 (包括學習新技術) | 120 | 240 |
· Design Spec | · 生成設計文檔 | 60 | 120 |
· Design Review | · 設計復審 (和同事審核設計文檔) | 60 | |
· Coding Standard | · 代碼規範 (為目前的開發制定合適的規範) | 0 | 0 |
· Design | · 具體設計 | 60 | 100 |
· Coding | · 具體編碼 | 60 | 100 |
· Code Review | · 代碼復審 | 20 | 20 |
· Test | · 測試(自我測試,修改代碼,提交修改) | 60 | 120 |
Reporting | 報告 | 30 | 30 |
· Test Report | · 測試報告 | 30 | 30 |
· Size Measurement | · 計算工作量 | 20 | 30 |
· Postmortem & Process Improvement Plan | · 事後總結, 並提出過程改進計劃 | 30 |
30 |
合計 | 880 |
1270 |
|
關鍵代碼
1 package wc_lhy; 2 import java.io.File; 3 import java.io.IOException; 4 import java.util.HashMap; 5 import java.util.Scanner; 6 import java.util.regex.Matcher; 7 import java.util.regex.Pattern; 8 9 10 public class Deal { 11 12 13 public static void main(String []args) throws IOException{ 14 15 HashMap<String, Boolean> orderMap = new HashMap<>(); 16 String standar; 17 Pattern pattern; 18 Matcher matcher; 19 20 orderMap.put("countLine", false); 21 orderMap.put("countChar", false); 22 orderMap.put("countWords", false); 23 orderMap.put("handleAll", false); 24 orderMap.put("complex", false); 25 26 //校驗格式 27 @SuppressWarnings("resource") 28 Scanner scanner = new Scanner(System.in); 29 String order = scanner.nextLine(); 30 //校驗標準 31 standar = "^wc.exe\\s\\-[wacsl]{1,3}\\s.{0,}"; 32 //匹配輸入格式是否正確 33 pattern = Pattern.compile(standar); 34 matcher = pattern.matcher(order); 35 if(!matcher.matches()){ 36 System.out.println("請輸入正確的命令格式"); 37 } 38 39 //校驗輸入格式正確後,對輸入命令按空格進行分割 40 String[] splitOrder = order.split("\\s+"); 41 42 /*對輸入命令分析需執行功能*/ 43 String string = "\\-[wscal]"; 44 //String path = splitOrder[splitOrder.length-1]; 45 String path = splitOrder[splitOrder.length-1]; 46 File file = new File(path); 47 for (int i = 1; i < splitOrder.length-1; i++) { 48 pattern = Pattern.compile(string); 49 Matcher matcher2 = pattern.matcher(splitOrder[i]); 50 if(matcher.matches()&&splitOrder[i].equals("-s")){ 51 orderMap.put("handleAll", true);//處理所有文件 52 } 53 else if (matcher2.matches()&&splitOrder[i].equals("-w")) { 54 orderMap.put("countWords", true);//處理單詞 55 } 56 else if(matcher2.matches()&&splitOrder[i].equals("-c")){ 57 orderMap.put("countChar", true);//處理字符 58 } 59 else if(matcher2.matches()&&splitOrder[i].equals("-l")){ 60 orderMap.put("countLine", true); 61 } 62 else if(matcher2.matches()&&splitOrder[i].equals("-a")){ 63 orderMap.put("complex", true);//復雜功能 64 } 65 else { 66 System.out.println("出錯!"); 67 } 68 } 69 70 Handle handle = new Handle(orderMap, path); 71 72 //-s路徑非文件夾 73 if(file.isDirectory()&&(orderMap.get("handleAll")==false)) System.out.println("非目錄操作!請輸入文件路徑"); 74 //-s操作+路徑為文件夾 75 if (orderMap.get("handleAll")&&handle.isFolder()) { 76 handle.handleFoler(path); 77 } 78 //-s操作但路徑不正確 79 else if((orderMap.get("handleAll") == true)&&(handle.isFolder() == false)){ 80 System.out.println("請輸入一個目錄!"); 81 } 82 //非-s操作且路徑非文件夾啊 83 else if((orderMap.get("handleAll") == false)&&(handle.isFolder() == false)){ 84 handle.judgeCount(path); 85 System.out.println("ok"); 86 } 87 88 } 89}
-s功能,實現對目錄所有文件的遞歸:
1 /*對文件夾進行的操作*/ 2 public void handleFoler(String folderPath){ 3 this.path = folderPath; 4 File file = new File(path); 5 //System.out.println("路徑為"+file.getAbsolutePath()); 6 File[] listFile = file.listFiles(); 7 if(listFile==null){ 8 return; 9 } 10 for(int i = 0; i< listFile.length; i++){ 11 //非文件夾,即可進行對文件進行處理 12 if(!listFile[i].isDirectory()){ 13 System.out.println("文件路徑:"+listFile[i].getAbsolutePath()); 14 System.out.println("文件名:"+listFile[i].getName()); 15 try{ 16 judgeCount(listFile[i].getAbsolutePath()); 17 } catch (IOException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 } 21 } 22 else{ 23 //為文件目錄,遞歸處理 24 System.out.println("文件目錄路徑"+file.getAbsolutePath()); 25 handleFoler(listFile[i].getAbsolutePath()); 26 } 27 28 } 29 }
-l 實現行數統計:
1 public void countLine(String path) throws IOException { 2 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(path)); 3 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 4 while (bufferedReader.read()!=-1) { 5 String string = bufferedReader.readLine(); 6 countLine++;//行數 7 } 8 bufferedReader.close(); 9 System.out.println("行數: "+getCountLine()); 10 }
-c 實現字符數統計:
1 public void countChar(String path) throws IOException { 2 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(path)); 3 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 4 while (bufferedReader.read()!=-1) { 5 String string = bufferedReader.readLine(); 6 if(string != null){ 7 countChar = countChar + string.length();//字符個數 8 } 9 } 10 bufferedReader.close(); 11 System.out.println("字符數: "+getCountChar()); 12 }
-w 實現單詞數統計:
1 public void countWords(String path) throws IOException { 2 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(path)); 3 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 4 while (bufferedReader.read()!=-1) { 5 String string = bufferedReader.readLine(); 6 7 if(string != null){ 8 countWords = countWords + string.split("\\s").length;//詞的個數 9 } 10 11 } 12 bufferedReader.close(); 13 System.out.println("單詞數:" + getCountWords()); 14 }
-a 統計代碼行、空行、註釋行:
1 //計算代碼行,註釋行。空白行 2 @SuppressWarnings("resource") 3 public void countComplex(String filePath) throws IOException { 4 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(filePath)); 5 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 6 //註釋行匹配 7 Pattern commetPattern = Pattern.compile("(^//.*$)|(^/\\*.*\\*/$)|(^/\\*\\*.*$)|(^\\*.*$)|.*\\*/$", 8 Pattern.MULTILINE + Pattern.DOTALL); 9 //空白行匹配 10 Pattern nullPattern = Pattern.compile("^\\s*$"); 11 //代碼行匹配 12 Pattern codePattern = Pattern.compile("\\s*", 13 Pattern.MULTILINE + Pattern.DOTALL); 14 //System.out.println(9); 15 String row; 16 while ((row = bufferedReader.readLine())!= null) { 17 if(nullPattern.matcher(row).find()){ 18 countNull++; 19 } 20 //註釋 21 if ((row!=null)&&commetPattern.matcher(row).matches()) { 22 commetLine++; 23 } 24 //代碼行 25 if((row!=null)&&codePattern.matcher(row).find()) { 26 //else { 27 codeLine++; 28 } 29 } 30 codeLine = codeLine - commetLine-countNull; 31 }
代碼測試:
測試文件:
運行結果:
測試文件:
運行結果:
測試文件:
項目總結:
完成了這個項目,感覺收獲還是挺多的。首先是加強了自己對java編程的熟悉度,還有就是正則表達式,以前只是囫圇吞棗得匆匆看了一遍,實戰是發現自己完全不會用,於是再去認真學習了一遍。在做這個項目的 過程中也是發現了自己存在的一些問題,首先就是算法不過關,考慮不全面,寫的也不夠精簡,所以常常會拋空指針的錯誤,不過有錯誤還是挺好的,解決自己的錯誤才能更好的進步,在以後的編程中才會更加的小心。還有就是在剛開始時會覺得無從下手,特別是對正則表達式不熟悉,不知道要從哪裏入手,因此開始編程浪費了挺多時間的。經過這次給了自己以後的一個小經驗:即使問題很難,也要嘗試著去動手。不要浪費時間去空想,著手,然後一個個的把這些問題解決掉。
java實現WC項目