java技術博客

          jsp博客
          數據加載中……
          本程序搜索文件中的字
          /**  (C) 北大青鳥APTECH.
           *   版權所有
           
          */


          /**
           * 本程序導入所需的類.
           
          */


          import java.io.File;
          import java.io.BufferedReader;
          import java.io.FileReader;
          import java.io.IOException;

          /**
           * 本程序搜索文件中的字.
           * 
          @version 1.0 2005 年 5 月 20 日
           * 
          @author Michael
           
          */


          class WordSearch {

          /** 存儲搜索模式. */
              String searchPattern;

          /** 創建一個 File 對象. */
              File fileObj;

          /** 
           *  構造方法.
           *  
          @param name 包含搜索模式
           *  
          @param fileName 包含要搜索的文件
           
          */

              WordSearch(String name, String fileName) 
          {

                 searchPattern 
          = name;
                 fileObj 
          = new File(fileName);
              }


          /** 
           * 執行搜索的方法.
           * 
          @param fileName 包含文件名
           * 
          @throws IOException 對象
           
          */

              
          void search(String fileName) throws IOException {

                   BufferedReader bufferObj 
          = new BufferedReader(
                   
          new FileReader(fileName));
                   String line;
                   
          int occurance = 0;
                   
          if (fileObj.exists() && fileObj.isFile()) {
                      
          while ((line = bufferObj.readLine()) != null{
                            
          if ((line.indexOf(searchPattern)) != -1{
                                 occurance
          ++;
                             }

                      }

                   }


                    
          if (occurance == 0{
                          System.out.println(
          "\n*******************");
                          System.out.println(
          "模式未找到");
                          System.out.println(
          "*******************");
                    }
           else {
                          System.out.println(
          "\n************************************");
                          System.out.println(
          "在文件 " + fileName + " 中找到的 " 
                              
          + searchPattern);

                    }

                    System.out.println();
                    System.out.println( 
          "文件 " + fileName +" 中 " 
                        
          + searchPattern + " 的出現次數是: " + occurance);
              }


          /** 
           * 顯示是文件或是目錄的方法.
           
          */

              
          void display() {
                   
          if (fileObj.exists() && fileObj.isDirectory()) {
                          System.out.println(
          "\n*************************");
                          System.out.println(
          "名稱: " + fileObj + " 是一個目錄");
                          System.out.println(
          "*************************");
                   }
           else  {
                         System.out.println(
          "");
                         System.out.println(
          "名稱: " + fileObj + " 是一個文件");
                         System.out.println(
          "********************************");
                   }

              }

          }


          /**
           * 本程序測試 WordSearch 類.
           * 
          @version 1.0 2005 年 5 月 20 日
           * 
          @author Michael
           
          */

          class WordSearchTest {

             
          /** 
              * 構造方法. 
              
          */

              
          protected WordSearchTest() {
              }


             
          /**
              * 這是一個 main 方法.
              * 
          @param args 被傳遞至 main 方法的參數
              
          */

              
          public static void main(String[] args)   {
                  
          if (args.length == 0 || args.length == 1)    {
                         System.out.println(
          "用法無效");
                         System.out.println(
          "用法 : java Search <pattern filename>");
                         
          return;
                  }

                  
          try {
                          WordSearch wordObj 
          = new WordSearch(args[0], args[1]);
                          wordObj.search(args[
          1]);
                          wordObj.display();
                      }
           catch (IOException e) {
                          System.out.println(
          "錯誤");
                      }


             }

          }
           

          posted on 2008-10-30 09:12 郭興華 閱讀(86) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 曲麻莱县| 昭觉县| 教育| 宁陵县| 乃东县| 洛宁县| 澎湖县| 清流县| 建瓯市| 遂昌县| 沭阳县| 开化县| 临汾市| 商都县| 德安县| 邹平县| 将乐县| 清苑县| 博湖县| 山西省| 西安市| 汝州市| 北辰区| 湄潭县| 上犹县| 武定县| 富裕县| 楚雄市| 静乐县| 固始县| 于田县| 九江市| 冷水江市| 湖口县| 汶川县| 色达县| 嵩明县| 任丘市| 南漳县| 漳州市| 台州市|