隨筆 - 117  文章 - 72  trackbacks - 0

          聲明:原創作品(標有[原]字樣)轉載時請注明出處,謝謝。

          常用鏈接

          常用設置
          常用軟件
          常用命令
           

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 155795
          • 排名 - 390

          最新評論

          [關鍵字]:java,design pattern,設計模式,《Java與模式》學習,decorator,裝飾模式,Unix
          [環境]:StarUML5.0 + JDK6
          [作者]:Winty (wintys@gmail.com)
          [正文]:

          package pattern.decorator.grep;

          import java.io.*;

          /**
           * 裝飾模式(Decorator Pattern):Grep
           *
           * Grep是UNIX中的命令,
           * 使用命令"grep BMW file",
           * 就可以在file中找出含有BMW的行,并顯示。
           *
           * @version 2009-6-6
           * @author Winty(wintys@gmail.com)
           */
          public class Grep{
              public static void main(String[] args)throws Exception{
                  GrepView view = new GrepView();

                  if(args.length < 2){
                      view.println("Usage: java Grep targetString fileName");
                      view.println("e.g.:java Grep BMW C:/sample.txt");
                      System.exit(0);
                  }
                  
                  GrepReader grep;

                  FileReader file = new FileReader(args[1]);
                  grep = new GrepReader(file , args[0] , view);
                  grep.search();
              }
          }

          class GrepReader extends FilterReader{
              private LineNumberReader reader;
              private String target;
              private GrepView view;

              /**
               *@param in 在輸入流in中查找目標字符串
               *@param target 要查找的目標字符串
               *@param view 查找結果輸出
               */
              public GrepReader(Reader in , String target , GrepView view){
                  super(in);
                  reader = new LineNumberReader(in);
                  this.target = target;
                  this.view = view;
              }

              public void search()throws Exception{
                  String line = null;
                  while((line = reader.readLine())!=null){
                      int index = -1;
                      if((index = line.indexOf(target)) != -1){
                          String str;
                          str = "line" + reader.getLineNumber() + " column" + (index +1) +":" + line;
                          view.println(str);
                      }
                  }

                  reader.close();
              }
          }

          class GrepView{
              private PrintStream out;

              public GrepView(){
                  this.out = System.out;
              }

              public void println(String str){
                  out.println(str);
              }
          }
          posted on 2009-06-07 22:42 天堂露珠 閱讀(999) 評論(2)  編輯  收藏 所屬分類: Pattern

          FeedBack:
          # re: [原]裝飾模式3-Grep 2009-06-07 23:22 subtitle
          good....  回復  更多評論
            
          # re: [原]裝飾模式3-Grep 2009-06-08 08:20 網絡小說
          ver ygood  回復  更多評論
            
          主站蜘蛛池模板: 古丈县| 巴林左旗| 丹江口市| 大田县| 宝山区| 福清市| 泾阳县| 武宣县| 松滋市| 德清县| 中西区| 浦东新区| 枣强县| 天祝| 博白县| 凌源市| 永昌县| 高唐县| 杭锦后旗| 台湾省| 崇仁县| 岑巩县| 吉木萨尔县| 镇原县| 社会| 会昌县| 宜宾市| 蛟河市| 江都市| 望江县| 淄博市| 梓潼县| 离岛区| 达日县| 墨玉县| 紫金县| 开江县| 景德镇市| 清水河县| 凤山市| 封开县|