數(shù)據(jù)加載中……
          本程序搜索文件中的字
          /**  (C) 北大青鳥APTECH.
           *   版權(quán)所有
           
          */


          /**
           * 本程序?qū)胨璧念?
           
          */


          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;

          /** 創(chuàng)建一個 File 對象. */
              File fileObj;

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

              WordSearch(String name, String fileName) 
          {

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


          /** 
           * 執(zhí)行搜索的方法.
           * 
          @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 + " 的出現(xiàn)次數(shù)是: " + 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 {

             
          /** 
              * 構(gòu)造方法. 
              
          */

              
          protected WordSearchTest() {
              }


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

              
          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 郭興華 閱讀(87) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 驻马店市| 丁青县| 新平| 正定县| 肇州县| 宽城| 德保县| 清丰县| 蒲城县| 扬中市| 蒙自县| 高雄县| 正蓝旗| 瑞安市| 乌拉特后旗| 资中县| 凤凰县| 弋阳县| 余干县| 应用必备| 岢岚县| 报价| 陕西省| 弋阳县| 同心县| 延津县| 长丰县| 贵州省| 文成县| 绥德县| 洞头县| 织金县| 阿拉善右旗| 寻乌县| 玉龙| 乳源| 通河县| 栖霞市| 章丘市| 乌拉特后旗| 嘉兴市|