數(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 {

          /** 存儲(chǔ)搜索模式. */
              String searchPattern;

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

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

              WordSearch(String name, String fileName) 
          {

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


          /** 
           * 執(zhí)行搜索的方法.
           * 
          @param fileName 包含文件名
           * 
          @throws IOException 對(duì)象
           
          */

              
          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 + " 是一個(gè)目錄");
                          System.out.println(
          "*************************");
                   }
           else  {
                         System.out.println(
          "");
                         System.out.println(
          "名稱: " + fileObj + " 是一個(gè)文件");
                         System.out.println(
          "********************************");
                   }

              }

          }


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

          class WordSearchTest {

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

              
          protected WordSearchTest() {
              }


             
          /**
              * 這是一個(gè) 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(
          "錯(cuò)誤");
                      }


             }

          }
           

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 安泽县| 东辽县| 鄂尔多斯市| 江阴市| 平顶山市| 莱芜市| 金阳县| 乌鲁木齐县| 平度市| 桃江县| 从江县| 班玛县| 延长县| 宣汉县| 南澳县| 随州市| 广昌县| 当阳市| 兰西县| 伊宁市| 兴文县| 江口县| 淳安县| 缙云县| 林芝县| 平果县| 会昌县| 吉林省| 沅陵县| 英吉沙县| 桂林市| 东乌珠穆沁旗| 濮阳县| 香港 | 永定县| 普宁市| 明星| 青岛市| 湄潭县| 织金县| 时尚|