github項目地址:https://github.com/BiuBiuBangBoom/wc
| PSP2.1 |
Personal Software Process Stages |
預估耗時(分鍾) |
實際耗時(分鍾) |
| Planning |
計划 |
30 | 40 |
| · Estimate |
· 估計這個任務需要多少時間 |
180 | 240 |
| Development |
開發 |
150 | 180 |
| · Analysis |
· 需求分析 (包括學習新技術) |
15 | 15 |
| · Design Spec |
· 生成設計文檔 |
20 | 20 |
| · Design Review |
· 設計復審 (和同事審核設計文檔) |
20 | 25 |
| · Coding Standard |
· 代碼規范 (為目前的開發制定合適的規范) |
10 | 15 |
| · Design |
· 具體設計 |
18 | 15 |
| · Coding |
· 具體編碼 |
120 | 150 |
| · Code Review |
· 代碼復審 |
30 | 35 |
| · Test |
· 測試(自我測試,修改代碼,提交修改) |
10 | 10 |
| Reporting |
報告 |
15 | 20 |
| · Test Report |
· 測試報告 |
15 | 15 |
| · Size Measurement |
· 計算工作量 |
12 | 30 |
| · Postmortem & Process Improvement Plan |
· 事后總結, 並提出過程改進計划 |
15 | 15 |
| 合計 |
635 | 825 |
一.項目簡介
能統計文本文件的字符數、單詞數和行數。這個項目要求寫一個命令行程序,模仿已有 wc.exe 的功能,並加以擴充,給出某程序設計語言源文件的字符數、單詞數和行數。實現一個統計程序,它能正確統計程序文件中的字符數、單詞數、行數,以及還具備其他擴展功能,並能夠快速地處理多個文件。
二.完成情況
基本功能:
- [x] -c <文件名> ,統計文件字符數。
- [x] -w <文件名> ,統計文件單詞數。
- [x] -l <文件名> ,統計文件行數數。
擴展功能
- [x] -s <文件路徑+類型(?任意)> ,遞歸符合條件的文件。
- [x] -a <文件名> ,統計文件代碼行,空白行,注釋行。
說明:以上命令可疊加使用,即 -c -w -l <文件名> ,統計文件的字符數,單詞數和行數。
三.解題思路
1.利用正則表達式校驗輸入參數,然后解析出相關參數和文件地址。
2.利用正則表達式和BufferReader的readline()方法進行各種計數
四.設計流程

項目中含有一個包,里面里有一個實現功能的主類,及一個測試用的類。其中主類中包含主函數,遞歸處理文件函數,行計數函數,詞計數函數,字符計數函數,拓展功能實現函數。
五.代碼說明
遞歸收集文件夾下符合要求的文件
public void getFolders(String filepath) { File file = new File(filepath); File[] folders = file.listFiles(); if (folders.length == 0) return; else for (File f : folders) { if (f.isDirectory()) getFolders(f.getAbsolutePath());//遞歸文件夾 else if (f.getName().endsWith("java")) files.add(f); } }
計算詞數
public void wordsCount(String filepath) throws IOException { int count = 0; File file = new File(filepath); BufferedReader br = new BufferedReader(new FileReader(file)); String line; Pattern pa = Pattern.compile("[\u4e00-\u9fa5]"); while((line = br.readLine()) != null) { String[] str = line.split("\\W+"); for(String word : str) { if(!word.equals("") && !pa.matcher(word).find())//檢查是否有中文或空字符 count++; } } System.out.println("詞數:" + count); br.close(); return; }
計算字符數
public void charCount(String filepath) throws IOException { int count = 0; File file = new File(filepath); BufferedReader br = new BufferedReader(new FileReader(file)); String line1, line2; while((line1 = br.readLine()) != null) { line2 = line1.replaceAll("\\s+", "");//將空白符刪去 count += line2.length(); } System.out.println("字符數:" + count); br.close(); return; }
計算行數
public void linesCount(String filepath) throws IOException { int count = 0; File file = new File(filepath); BufferedReader br = new BufferedReader(new FileReader(file)); while(br.readLine() != null) count++; System.out.println("行數:" + count); br.close(); return; }
拓展功能實現,計算空白行,注釋行,代碼行
public void advancedCount(String filepath) throws IOException { int emptyLineCount = 0; int codeLineCount = 0; int comLineCount = 0; File file = new File(filepath); BufferedReader br = new BufferedReader(new FileReader(file)); String line; while((line = br.readLine()) != null) { if(Pattern.compile("^\\s*\\S?\\s*$").matcher(line).matches())//檢驗空行 emptyLineCount++; else if(line.contains("//")) comLineCount++; else codeLineCount++; } System.out.println("空行:" + emptyLineCount); System.out.println("代碼行:" + codeLineCount); System.out.println("注釋行:" + comLineCount); br.close(); return; }
六.測試
程序啟動

測試空文件


只有一個字符的文件


遞歸文件調用

七.項目總結
通過本次項目,我對於從前比較模糊的文件輸入跟正則表達式有了較好的掌握,同時也對字符串類型方法有了更深的了解。在開發過程中,我經常遇到不知道如何實現某個功能,但通過查看文檔往往會發現有很多很好用的方法,使我對功能的實現有了靈感。就此我深感文檔對於開發的重要性。
