Decorative Mode 3-grep

Source: Internet
Author: User

[keywords]:java,design pattern, design patterns, "Java and schema" learning, decorator, decoration mode, Unix

[Environment]:staruml5.0 + JDK6

[Author]:winty (wintys@gmail.com)

[Body]:

 Package Pattern.decorator.grep Import java.io.*/** * Decorative mode (decorator pattern): grep * * grep is a command in UNIX, * use command
 grep BMW File ", * you can find the line containing BMW in file, and show. * * @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 the input stream in to find the target string * @param target to find the destination string * @param view Lookup results Output/public Grepreader (Re Ader in, String Target, GRepview view) {super (in);
        reader = new LineNumberReader (in);
        This.target = target;
    This.view = view;
        public void Search () throws exception{String = 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); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.