(第二次作業)運用Java統計字符數、單詞數、行數


---恢復內容開始---

                                           Gitee項目地址:項目地址

                                      https://gitee.com/ZK154/WordCount.git                         

1.解題思路

       

首先,當我讀完作業要求后,我有了一個大概的思路。   這是一個IO流的問題,有文件的讀與寫,有以下幾點需要注意:

  •  判斷關鍵輸入符是否存在(-c , -w ,-l , -o)。
  •    判斷讀入文件是否存在 。
  •    判斷寫入文件是否存在。
  •    判斷控制台輸入格式是否正確 。
  •    對文件內容要用正則表達式篩選。

     這幾點分開看並不難,但是合起來比較亂。對文件中其他字符、字母的查找、漢字的去除要用到正則表達式,因為要求生成.exe可執行文件,所以我選擇java編程

  

 

2.准備工作

  •  下載Myeclipse / eclipse / Sublime Text3  任選一樣
  •  下載JDK , java編程的核心 。    JDK下載地址
  •    下載exe4j 用於生成exe文件 。 exe4j下載 

 

3.實現編碼

  為了實現功能,我寫了6個靜態函數

int  linewNum(String fileName)                //接受文件名,返回行數,  
int wordNum(String fileName )                 //接受文件名 ,返回單詞數,
int characterNum(String fileName )             //接受文件名,返回字符數,    
void writeFile(String targetFile, String save)           //接受目標文件與要保存的數據
void writeResult(String string)              //接受保存的數據,保存進默認的result.txt文件
String selectC(String[] str, String target)          //查詢.c文件名,並返回

每個函數都符合“單入口,單出口”的結構化程序設計原則,只需傳遞一個參數就可以完成功能,代碼復用性很強。

說明:

控制台輸入進來的就是一個字符串,例如“wc.exe -w test.c ”,我們想要獲得要查詢的文件名“test.c”就要對字符串進行分割 java里Object.split( )可以現,返回一個數組。同理 “-w”,"-c","-l","-o"這些關鍵字也可以獲得,這樣根據這些字符就可以進行功能的實現。

 

4.具體代碼

4.1實現對字符的統計

 按字節讀取先存入char數組里,再進行統計,注意:回車包含兩個符號"\r\n"

 1 public static int characterNum(String fileName) throws IOException {
 2         File file = new File(fileName);
 3         int x = 0;
 4         if (file.exists()) {
 5             String path = file.getAbsolutePath();// 得到exe的文件路徑
 6             FileReader fr = new FileReader(path);
 7             BufferedReader br = new BufferedReader(fr);
 8             String str;
 9             char[] ch = new char[300000];
10             int len = 0;
11 
12             while ((len = br.read(ch)) != -1) {
13                 x = len;
14                 // System.out.println(ch[len]);
15             }
16             fr.close();
17             br.close();
18             System.out.println("字符數:" + x);
19             writeResult("字符數:" + x + "\r\n");
20             if (!(x != 0)) {
21                 x = x + 1;
22             }
23         }
24 
25         return x;
26     }
characterNum

 

4.2實現對單詞的統計

  1. 題目要求"," 和" "分格的算單詞,先用正則表達式過濾漢字,再將"," 替換成空格,這樣就能分清哪些是單詞,在用split按空格分割,單詞就出來了
 1     // 單詞數
 2     public static int wordNum(String fileName) throws IOException {
 3         File file = new File(fileName);
 4         int total = 0;
 5         if (file.exists()) {
 6             String path = file.getAbsolutePath();// 得到exe的文件路徑
 7             FileReader fr = new FileReader(path);
 8             BufferedReader br = new BufferedReader(fr);
 9             String str;
10             int line = 0;
11             ArrayList array = new ArrayList();
12             while ((str = br.readLine()) != null) {
13                 str = str.replaceAll("[(\\u4e00-\\u9fa5)]", "");// 去除漢字
14                 str = str.replaceAll(",", " "); // 去除空格
15                 String[] str1 = str.split("\\s+"); // 按空格分割
16                 array.add(str1); // 放入集合
17                 line++;
18             }
19             fr.close();
20             br.close();
21 
22             String regex = ".*[a-zA-Z]+.*"; // 正則判斷每個數組中是否存在有效單詞(存在字母)
23             Pattern p = Pattern.compile(regex);
24             Iterator it = array.iterator();
25             while (it.hasNext()) {
26                 String[] string = (String[]) it.next();
27                 for (int y = 0; y <= string.length - 1; y++) {
28                     Matcher m = p.matcher(string[y]);
29                     if (m.matches()) {
30                         total++; // 每存在一個total加1
31                     }
32                 }
33             }
34             System.out.println("單詞數:" + total);
35             writeResult("單詞數:" + total + "\r\n");
36             if (!(total != 0)) {
37                 total = total + 1;
38             }
39         }
40 
41         return total;
42     }
wordNum

 

4.3實現對行數的統計

java File類自帶函數readline可以按行讀取,定義一個int形變量 ,每讀一行則加一

 1     // 行數
 2     public static int lineNum(String fileName) throws IOException {
 3         File file = new File(fileName);
 4         int line = 0;
 5         if (file.exists()) {
 6             String path = new File(fileName).getAbsolutePath();// 得到exe的文件路徑
 7             FileReader fr = new FileReader(path);
 8             BufferedReader br = new BufferedReader(fr);
 9             while (br.readLine() != null) { // 按行讀取,每存在一行line+1
10                 line++;
11             }
12             fr.close();
13             br.close();
14             System.out.println("行數:" + line);
15             writeResult("行數:" + line + "\r\n");
16             if (!(line != 0)) {
17                 line = line + 1;
18             }
19         }
20 
21         return line;
22     }
lineNum

 

4.4實現對默認result.txt的寫入

現獲取result.txt的地址,沒有的話java里的FileWrite自動生成result.txt文件

 1 // 自動寫入result.txt文件中
 2     public static void writeResult(String string) throws IOException {
 3 
 4         String path = new File("result.txt").getAbsolutePath();
 5         FileWriter fw = new FileWriter(path, true);
 6         fw.write(string);
 7         if (fw != null) {
 8             fw.close();
 9         }
10     }
writeResult

 

4.5實現對指定文本的寫入

接受目標文件名,並獲得路徑,,創建出的filewrite對象使用write( )方法存入接受的信息字符串

 1 // 寫進指定文件
 2     public static void writeFile(String targetFile, String save) throws IOException {
 3 
 4         String path = new File(targetFile).getAbsolutePath(); // 得到目標文件路徑
 5         FileWriter fw = new FileWriter(path, true);
 6         fw.write(save);
 7         if (fw != null) {
 8             fw.close();
 9             System.out.println("存儲成功!");
10         }
11 
12     }
writeFile

 

 

 

 

5.代碼測試

  測試文件“test.c”,寫入文件output.txt,默認的result.txt與wc.exe都在同一目錄下。但寫入文件需要存在

 

5.1 測試對行數、字符數、單詞數單獨查詢

 

 

 

 

 

5.2 測試關鍵字符的聯合查詢,因為是添加模式,result.txt里信息沒有被覆蓋

 

 

 

5.3 測試對指定文件的寫入(-o)

 

 

 

 

 

5.4 測試代碼能否辨別錯誤:

 

 6.單元測試

  將程序中最小的模塊拿出來進行測試,

6.1對characterNum測試:結果正確無誤

 

 

 

 

6.2 對wordNum進行測試:符合結果通過

 

 

 

6.3 對lineNum函數進行測試:

 

 

6.4 對writeFile函數的測試:

 

 

6.5 對writeResult函數的測試:

測試結果:各函數在單獨執行下都能完成功能。

 

7.個人感悟

  通過在作業中對代碼的編寫的過程中,我不僅僅為了完成代碼的功能,同事也在考慮如何用軟件工程的思想來看待它,如何提高代碼的復用性 ? 怎樣降低代碼的耦合度 ? 邏輯中要做哪些健壯性的判斷 ? 那些步驟需要提高程序的友好度? 這些問題都是在軟件工程這個大角度來完善程序設計,這些思考方式與訓練是我編寫中最多的收獲,更讓我明白高質量的編程才是最終目的。

    8.參考資料

             

           1. 如何將jar打包成exe文件

 

                     2.Java io流最詳細講解

 

               3.JAVA String類常用方法詳解

 

               4.  關於Java的單元測試

---恢復內容結束---


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM